shoulda-matchers 2.6.1 → 2.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +9 -0
- data/.yardopts +8 -0
- data/Appraisals +82 -33
- data/Gemfile +18 -2
- data/Gemfile.lock +13 -1
- data/NEWS.md +27 -2
- data/README.md +83 -1329
- data/Rakefile +118 -1
- data/cucumber.yml +1 -0
- data/doc_config/gh-pages/index.html.erb +9 -0
- data/doc_config/yard/setup.rb +22 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/bootstrap.css +5967 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/full_list.css +12 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/global.css +45 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/solarized.css +69 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/style.css +283 -0
- data/doc_config/yard/templates/default/fulldoc/html/full_list.erb +32 -0
- data/doc_config/yard/templates/default/fulldoc/html/full_list_class.erb +1 -0
- data/doc_config/yard/templates/default/fulldoc/html/full_list_method.erb +8 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/app.js +298 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/full_list.js +1 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/jquery.stickyheaders.js +289 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/underscore.min.js +6 -0
- data/doc_config/yard/templates/default/fulldoc/html/setup.rb +8 -0
- data/doc_config/yard/templates/default/layout/html/breadcrumb.erb +14 -0
- data/doc_config/yard/templates/default/layout/html/fonts.erb +1 -0
- data/doc_config/yard/templates/default/layout/html/layout.erb +23 -0
- data/doc_config/yard/templates/default/layout/html/search.erb +13 -0
- data/doc_config/yard/templates/default/layout/html/setup.rb +40 -0
- data/doc_config/yard/templates/default/method_details/html/source.erb +10 -0
- data/doc_config/yard/templates/default/module/html/box_info.erb +31 -0
- data/docs.watchr +5 -0
- data/features/rails_integration.feature +32 -0
- data/features/step_definitions/rails_steps.rb +55 -9
- data/features/support/env.rb +1 -0
- data/gemfiles/3.0.gemfile +13 -1
- data/gemfiles/3.0.gemfile.lock +13 -1
- data/gemfiles/3.1.gemfile +17 -2
- data/gemfiles/3.1.gemfile.lock +31 -2
- data/gemfiles/3.1_1.9.2.gemfile +33 -0
- data/gemfiles/3.1_1.9.2.gemfile.lock +203 -0
- data/gemfiles/3.2.gemfile +18 -2
- data/gemfiles/3.2.gemfile.lock +32 -2
- data/gemfiles/3.2_1.9.2.gemfile +32 -0
- data/gemfiles/3.2_1.9.2.gemfile.lock +200 -0
- data/gemfiles/4.0.0.gemfile +20 -1
- data/gemfiles/4.0.0.gemfile.lock +46 -2
- data/gemfiles/4.0.1.gemfile +20 -1
- data/gemfiles/4.0.1.gemfile.lock +46 -2
- data/gemfiles/4.1.gemfile +21 -2
- data/gemfiles/4.1.gemfile.lock +47 -4
- data/lib/shoulda/matchers/action_controller.rb +0 -20
- data/lib/shoulda/matchers/action_controller/callback_matcher.rb +119 -28
- data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +22 -6
- data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +43 -10
- data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +40 -13
- data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +63 -11
- data/lib/shoulda/matchers/action_controller/rescue_from_matcher.rb +34 -1
- data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +84 -15
- data/lib/shoulda/matchers/action_controller/route_matcher.rb +84 -28
- data/lib/shoulda/matchers/action_controller/route_params.rb +4 -3
- data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +76 -13
- data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +147 -13
- data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +148 -2
- data/lib/shoulda/matchers/active_model.rb +0 -25
- data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +66 -9
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +161 -19
- data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +5 -5
- data/lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb +92 -13
- data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +218 -16
- data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +198 -32
- data/lib/shoulda/matchers/active_model/errors.rb +5 -2
- data/lib/shoulda/matchers/active_model/exception_message_finder.rb +1 -1
- data/lib/shoulda/matchers/active_model/have_secure_password_matcher.rb +29 -8
- data/lib/shoulda/matchers/active_model/helpers.rb +20 -8
- data/lib/shoulda/matchers/active_model/numericality_matchers.rb +9 -0
- data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +4 -6
- data/lib/shoulda/matchers/active_model/numericality_matchers/even_number_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher.rb +3 -2
- data/lib/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb +52 -14
- data/lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb +51 -13
- data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +53 -7
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +275 -19
- data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +84 -14
- data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +170 -41
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +20 -15
- data/lib/shoulda/matchers/active_model/validation_message_finder.rb +1 -2
- data/lib/shoulda/matchers/active_record.rb +1 -12
- data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +89 -15
- data/lib/shoulda/matchers/active_record/association_matcher.rb +726 -70
- data/lib/shoulda/matchers/active_record/association_matchers.rb +9 -0
- data/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/inverse_of_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +2 -1
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +4 -5
- data/lib/shoulda/matchers/active_record/association_matchers/order_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/source_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +79 -15
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +64 -15
- data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +21 -7
- data/lib/shoulda/matchers/active_record/serialize_matcher.rb +85 -10
- data/lib/shoulda/matchers/assertion_error.rb +7 -1
- data/lib/shoulda/matchers/doublespeak.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/double.rb +3 -1
- data/lib/shoulda/matchers/doublespeak/double_collection.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/double_implementation_registry.rb +1 -0
- data/lib/shoulda/matchers/doublespeak/object_double.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/proxy_implementation.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/structs.rb +2 -0
- data/lib/shoulda/matchers/doublespeak/stub_implementation.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/world.rb +3 -4
- data/lib/shoulda/matchers/error.rb +1 -0
- data/lib/shoulda/matchers/independent/delegate_matcher.rb +108 -20
- data/lib/shoulda/matchers/independent/delegate_matcher/stubbed_target.rb +4 -3
- data/lib/shoulda/matchers/integrations/nunit_test_case_detection.rb +3 -0
- data/lib/shoulda/matchers/rails_shim.rb +3 -2
- data/lib/shoulda/matchers/version.rb +2 -1
- data/lib/shoulda/matchers/warn.rb +1 -0
- data/script/SUPPORTED_VERSIONS +1 -0
- data/script/install_gems_in_all_appraisals +14 -0
- data/script/run_all_tests +14 -0
- data/shoulda-matchers.gemspec +0 -10
- data/spec/report_warnings.rb +7 -0
- data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +1 -1
- data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +9 -0
- data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +0 -36
- data/spec/shoulda/matchers/active_model/validation_message_finder_spec.rb +2 -2
- data/spec/shoulda/matchers/doublespeak/double_spec.rb +1 -1
- data/spec/shoulda/matchers/doublespeak/world_spec.rb +11 -29
- data/spec/shoulda/matchers/doublespeak_spec.rb +3 -3
- data/spec/spec_helper.rb +17 -0
- data/spec/support/class_builder.rb +4 -0
- data/spec/support/test_application.rb +1 -1
- data/spec/warnings_spy.rb +64 -0
- data/spec/warnings_spy/filesystem.rb +45 -0
- data/spec/warnings_spy/partitioner.rb +29 -0
- data/spec/warnings_spy/reader.rb +64 -0
- data/spec/warnings_spy/reporter.rb +87 -0
- metadata +49 -134
@@ -1,25 +1,49 @@
|
|
1
|
-
module Shoulda
|
1
|
+
module Shoulda
|
2
2
|
module Matchers
|
3
|
-
module ActionController
|
4
|
-
#
|
3
|
+
module ActionController
|
4
|
+
# The `render_template` matcher tests that an action renders a template
|
5
|
+
# or partial. In RSpec, it is very similar to rspec-rails's
|
6
|
+
# `render_template` matcher. In Test::Unit, it provides a more expressive
|
7
|
+
# syntax over `assert_template`.
|
5
8
|
#
|
6
|
-
#
|
9
|
+
# class PostsController < ApplicationController
|
10
|
+
# def show
|
11
|
+
# end
|
12
|
+
# end
|
7
13
|
#
|
8
|
-
#
|
14
|
+
# # app/views/posts/show.html.erb
|
15
|
+
# <%= render 'sidebar' %>
|
9
16
|
#
|
10
|
-
#
|
11
|
-
#
|
17
|
+
# # RSpec
|
18
|
+
# describe PostsController do
|
19
|
+
# describe 'GET #show' do
|
20
|
+
# before { get :show }
|
12
21
|
#
|
13
|
-
#
|
14
|
-
#
|
22
|
+
# it { should render_template('show') }
|
23
|
+
# it { should render_template(partial: 'sidebar') }
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# # Test::Unit
|
28
|
+
# class PostsControllerTest < ActionController::TestCase
|
29
|
+
# context 'GET #show' do
|
30
|
+
# setup { get :show }
|
31
|
+
#
|
32
|
+
# should render_template('show')
|
33
|
+
# should render_template(partial: 'sidebar')
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
#
|
38
|
+
#
|
39
|
+
# @return [RenderTemplateMatcher]
|
15
40
|
#
|
16
|
-
# assert that no partials were rendered
|
17
|
-
# it { should render_template(partial: false) }
|
18
41
|
def render_template(options = {}, message = nil)
|
19
42
|
RenderTemplateMatcher.new(options, message, self)
|
20
43
|
end
|
21
44
|
|
22
|
-
|
45
|
+
# @private
|
46
|
+
class RenderTemplateMatcher
|
23
47
|
attr_reader :failure_message, :failure_message_when_negated
|
24
48
|
|
25
49
|
alias failure_message_for_should failure_message
|
@@ -29,7 +53,10 @@ module Shoulda # :nodoc:
|
|
29
53
|
@options = options
|
30
54
|
@message = message
|
31
55
|
@template = options.is_a?(Hash) ? options[:partial] : options
|
32
|
-
@context
|
56
|
+
@context = context
|
57
|
+
@controller = nil
|
58
|
+
@failure_message = nil
|
59
|
+
@failure_message_when_negated = nil
|
33
60
|
end
|
34
61
|
|
35
62
|
def matches?(controller)
|
@@ -1,24 +1,76 @@
|
|
1
|
-
module Shoulda
|
1
|
+
module Shoulda
|
2
2
|
module Matchers
|
3
|
-
module ActionController
|
4
|
-
|
5
|
-
#
|
3
|
+
module ActionController
|
4
|
+
# The `render_with_layout` matcher asserts that an action is rendered with
|
5
|
+
# a particular layout.
|
6
|
+
#
|
7
|
+
# class PostsController < ApplicationController
|
8
|
+
# def show
|
9
|
+
# render layout: 'posts'
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# # RSpec
|
14
|
+
# describe PostsController do
|
15
|
+
# describe 'GET #show' do
|
16
|
+
# before { get :show }
|
17
|
+
#
|
18
|
+
# it { should render_with_layout('posts') }
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# # Test::Unit
|
23
|
+
# class PostsControllerTest < ActionController::TestCase
|
24
|
+
# context 'GET #show' do
|
25
|
+
# setup { get :show }
|
26
|
+
#
|
27
|
+
# should render_with_layout('posts')
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# It can also be used to assert that the action is not rendered with a
|
32
|
+
# layout at all:
|
33
|
+
#
|
34
|
+
# class PostsController < ApplicationController
|
35
|
+
# def sidebar
|
36
|
+
# render layout: false
|
37
|
+
# end
|
38
|
+
# end
|
6
39
|
#
|
7
|
-
#
|
40
|
+
# # RSpec
|
41
|
+
# describe PostsController do
|
42
|
+
# describe 'GET #sidebar' do
|
43
|
+
# before { get :sidebar }
|
44
|
+
#
|
45
|
+
# it { should_not render_with_layout }
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# # Test::Unit
|
50
|
+
# class PostsControllerTest < ActionController::TestCase
|
51
|
+
# context 'GET #sidebar' do
|
52
|
+
# setup { get :sidebar }
|
53
|
+
#
|
54
|
+
# should_not render_with_layout
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# @return [RenderWithLayoutMatcher]
|
8
59
|
#
|
9
|
-
# it { should render_with_layout }
|
10
|
-
# it { should render_with_layout(:special) }
|
11
|
-
# it { should_not render_with_layout }
|
12
60
|
def render_with_layout(expected_layout = nil)
|
13
61
|
RenderWithLayoutMatcher.new(expected_layout).in_context(self)
|
14
62
|
end
|
15
63
|
|
16
|
-
|
17
|
-
|
64
|
+
# @private
|
65
|
+
class RenderWithLayoutMatcher
|
18
66
|
def initialize(expected_layout)
|
19
|
-
|
67
|
+
if expected_layout
|
20
68
|
@expected_layout = expected_layout.to_s
|
69
|
+
else
|
70
|
+
@expected_layout = nil
|
21
71
|
end
|
72
|
+
|
73
|
+
@controller = nil
|
22
74
|
end
|
23
75
|
|
24
76
|
# Used to provide access to layouts recorded by
|
@@ -1,13 +1,46 @@
|
|
1
1
|
module Shoulda
|
2
2
|
module Matchers
|
3
3
|
module ActionController
|
4
|
+
# The `rescue_from` matcher tests usage of the `rescue_from` macro. It
|
5
|
+
# asserts that an exception and method are present in the list of
|
6
|
+
# exception handlers, and that the handler method exists.
|
7
|
+
#
|
8
|
+
# class ApplicationController < ActionController::Base
|
9
|
+
# rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
|
10
|
+
#
|
11
|
+
# private
|
12
|
+
#
|
13
|
+
# def handle_not_found
|
14
|
+
# # ...
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # RSpec
|
19
|
+
# describe ApplicationController do
|
20
|
+
# it do
|
21
|
+
# should rescue_from(ActiveRecord::RecordNotFound).
|
22
|
+
# with(:handle_not_found)
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# # Test::Unit
|
27
|
+
# class ApplicationControllerTest < ActionController::TestCase
|
28
|
+
# should rescue_from(ActiveRecord::RecordNotFound).
|
29
|
+
# with(:handle_not_found)
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @return [RescueFromMatcher]
|
33
|
+
#
|
4
34
|
def rescue_from(exception)
|
5
35
|
RescueFromMatcher.new exception
|
6
36
|
end
|
7
37
|
|
38
|
+
# @private
|
8
39
|
class RescueFromMatcher
|
9
40
|
def initialize(exception)
|
10
41
|
@exception = exception
|
42
|
+
@expected_method = nil
|
43
|
+
@controller = nil
|
11
44
|
end
|
12
45
|
|
13
46
|
def with(method)
|
@@ -36,7 +69,7 @@ module Shoulda
|
|
36
69
|
end
|
37
70
|
alias failure_message_for_should_not failure_message_when_negated
|
38
71
|
|
39
|
-
|
72
|
+
protected
|
40
73
|
|
41
74
|
attr_reader :controller, :exception, :expected_method, :handlers
|
42
75
|
|
@@ -1,26 +1,95 @@
|
|
1
|
-
module Shoulda
|
1
|
+
module Shoulda
|
2
2
|
module Matchers
|
3
|
-
module ActionController
|
4
|
-
|
5
|
-
#
|
3
|
+
module ActionController
|
4
|
+
# The `respond_with` matcher tests that an action responds with a certain
|
5
|
+
# status code.
|
6
|
+
#
|
7
|
+
# You can specify that the status should be a number:
|
8
|
+
#
|
9
|
+
# class PostsController < ApplicationController
|
10
|
+
# def index
|
11
|
+
# render status: 403
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # RSpec
|
16
|
+
# describe PostsController do
|
17
|
+
# describe 'GET #index' do
|
18
|
+
# before { get :index }
|
19
|
+
#
|
20
|
+
# it { should respond_with(403) }
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # Test::Unit
|
25
|
+
# class PostsControllerTest < ActionController::TestCase
|
26
|
+
# context 'GET #index' do
|
27
|
+
# setup { get :index }
|
28
|
+
#
|
29
|
+
# should respond_with(403)
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# You can specify that the status should be within a range of numbers:
|
34
|
+
#
|
35
|
+
# class PostsController < ApplicationController
|
36
|
+
# def destroy
|
37
|
+
# render status: 508
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# # RSpec
|
42
|
+
# describe PostsController do
|
43
|
+
# describe 'DELETE #destroy' do
|
44
|
+
# before { delete :destroy }
|
6
45
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
46
|
+
# it { should respond_with(500..600) }
|
47
|
+
# end
|
48
|
+
# end
|
10
49
|
#
|
11
|
-
#
|
50
|
+
# # Test::Unit
|
51
|
+
# class PostsControllerTest < ActionController::TestCase
|
52
|
+
# context 'DELETE #destroy' do
|
53
|
+
# setup { delete :destroy }
|
54
|
+
#
|
55
|
+
# should respond_with(500..600)
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# Finally, you can specify that the status should be a symbol:
|
60
|
+
#
|
61
|
+
# class PostsController < ApplicationController
|
62
|
+
# def show
|
63
|
+
# render status: :locked
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# # RSpec
|
68
|
+
# describe PostsController do
|
69
|
+
# describe 'GET #show' do
|
70
|
+
# before { get :show }
|
71
|
+
#
|
72
|
+
# it { should respond_with(:locked) }
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# # Test::Unit
|
77
|
+
# class PostsControllerTest < ActionController::TestCase
|
78
|
+
# context 'GET #show' do
|
79
|
+
# setup { get :show }
|
80
|
+
#
|
81
|
+
# should respond_with(:locked)
|
82
|
+
# end
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# @return [RespondWithMatcher]
|
12
86
|
#
|
13
|
-
# it { should respond_with(:success) }
|
14
|
-
# it { should respond_with(:redirect) }
|
15
|
-
# it { should respond_with(:missing) }
|
16
|
-
# it { should respond_with(:error) }
|
17
|
-
# it { should respond_with(501) }
|
18
87
|
def respond_with(status)
|
19
88
|
RespondWithMatcher.new(status)
|
20
89
|
end
|
21
90
|
|
22
|
-
|
23
|
-
|
91
|
+
# @private
|
92
|
+
class RespondWithMatcher
|
24
93
|
def initialize(status)
|
25
94
|
@status = symbol_to_status_code(status)
|
26
95
|
end
|
@@ -1,36 +1,92 @@
|
|
1
|
-
module Shoulda
|
1
|
+
module Shoulda
|
2
2
|
module Matchers
|
3
|
-
module ActionController
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
3
|
+
module ActionController
|
4
|
+
# The `route` matcher tests that a route resolves to a controller,
|
5
|
+
# action, and params; and that the controller, action, and params
|
6
|
+
# generates the same route. For an RSpec suite, this is like using a
|
7
|
+
# combination of `route_to` and `be_routable`. For a Test::Unit suite, it
|
8
|
+
# provides a more expressive syntax over `assert_routing`.
|
9
|
+
#
|
10
|
+
# You can use this matcher either in a controller test case or in a
|
11
|
+
# routing test case. For instance, given these routes:
|
12
|
+
#
|
13
|
+
# My::Application.routes.draw do
|
14
|
+
# get '/posts', controller: 'posts', action: 'index'
|
15
|
+
# get '/posts/:id' => 'posts#show'
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# You could choose to write tests for these routes alongside other tests
|
19
|
+
# for PostsController:
|
20
|
+
#
|
21
|
+
# class PostsController < ApplicationController
|
22
|
+
# # ...
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# # RSpec
|
26
|
+
# describe PostsController do
|
27
|
+
# it { should route(:get, '/posts').to(action: :index) }
|
28
|
+
# it { should route(:get, '/posts/1').to(action: :show, id: 1) }
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# # Test::Unit
|
32
|
+
# class PostsControllerTest < ActionController::TestCase
|
33
|
+
# should route(:get, '/posts').to(action: 'index')
|
34
|
+
# should route(:get, '/posts/1').to(action: :show, id: 1)
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# Or you could place the tests along with other route tests:
|
38
|
+
#
|
39
|
+
# # RSpec
|
40
|
+
# describe 'Routing' do
|
41
|
+
# it do
|
42
|
+
# should route(:get, '/posts').
|
43
|
+
# to(controller: :posts, action: :index)
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# it do
|
47
|
+
# should route(:get, '/posts/1').
|
48
|
+
# to('posts#show', id: 1)
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# # Test::Unit
|
53
|
+
# class RoutesTest < ActionController::IntegrationTest
|
54
|
+
# should route(:get, '/posts').
|
55
|
+
# to(controller: :posts, action: :index)
|
56
|
+
#
|
57
|
+
# should route(:get, '/posts/1').
|
58
|
+
# to('posts#show', id: 1)
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# Notice that in the former case, as we are inside of a test case for
|
62
|
+
# PostsController, we do not have to specify that the routes resolve to
|
63
|
+
# this controller. In the latter case we specify this using the
|
64
|
+
# `controller` key passed to the `to` qualifier.
|
65
|
+
#
|
66
|
+
# #### Qualifiers
|
67
|
+
#
|
68
|
+
# ##### to
|
69
|
+
#
|
70
|
+
# Use `to` to specify the action (along with the controller, if needed)
|
71
|
+
# that the route resolves to.
|
72
|
+
#
|
73
|
+
# # Three ways of saying the same thing (using the example above)
|
74
|
+
# route(:get, '/posts').to(action: index)
|
75
|
+
# route(:get, '/posts').to(controller: :posts, action: index)
|
76
|
+
# route(:get, '/posts').to('posts#index')
|
77
|
+
#
|
78
|
+
# If there are parameters in your route, then specify those too:
|
79
|
+
#
|
80
|
+
# route(:get, '/posts/1').to('posts#show', id: 1)
|
81
|
+
#
|
82
|
+
# @return [RouteMatcher]
|
83
|
+
#
|
29
84
|
def route(method, path)
|
30
85
|
RouteMatcher.new(method, path, self)
|
31
86
|
end
|
32
87
|
|
33
|
-
|
88
|
+
# @private
|
89
|
+
class RouteMatcher
|
34
90
|
def initialize(method, path, context)
|
35
91
|
@method = method
|
36
92
|
@path = path
|