Module: ActionDispatch::Assertions::RoutingAssertions
- Included in:
- ActionDispatch::Assertions
- Defined in:
- actionpack/lib/action_dispatch/testing/assertions/routing.rb
Overview
Suite of assertions to test routes generated by Rails and the handling of requests made to them.
Instance Method Summary collapse
-
#assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that the provided options can be used to generate the provided path.
-
#assert_recognizes(expected_options, path, extras = {}, message = nil) ⇒ Object
Asserts that the routing of the given
path
was handled correctly and that the parsed options (given in theexpected_options
hash) matchpath
. -
#assert_routing(path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that path and options match both ways; in other words, it verifies that
path
generatesoptions
and then thatoptions
generatespath
. -
#method_missing(selector, *args, &block) ⇒ Object
ROUTES TODO: These assertions should really work in an integration context.
-
#with_routing ⇒ Object
A helper to make it easier to test different route configurations.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(selector, *args, &block) ⇒ Object
ROUTES TODO: These assertions should really work in an integration context
171 172 173 174 175 176 177 |
# File 'actionpack/lib/action_dispatch/testing/assertions/routing.rb', line 171 def method_missing(selector, *args, &block) if defined?(@controller) && @controller && @routes && @routes.named_routes.helpers.include?(selector) @controller.send(selector, *args, &block) else super end end |
Instance Method Details
#assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes
. The extras
parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message
parameter allows you to specify a custom error message for assertion failures.
The defaults
parameter is unused.
Examples
# Asserts that the default action is generated for a route with no action
assert_generates "/items", :controller => "items", :action => "index"
# Tests that the list action is properly routed
assert_generates "/items/list", :controller => "items", :action => "list"
# Tests the generation of a route with a parameter
assert_generates "/items/list/1", { :controller => "items", :action => "list", :id => "1" }
# Asserts that the generated route gives us our custom route
assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'actionpack/lib/action_dispatch/testing/assertions/routing.rb', line 70 def assert_generates(expected_path, , defaults={}, extras = {}, =nil) if expected_path =~ %r{://} begin uri = URI.parse(expected_path) expected_path = uri.path.to_s.empty? ? "/" : uri.path rescue URI::InvalidURIError => e raise ActionController::RoutingError, e. end else expected_path = "/#{expected_path}" unless expected_path.first == '/' end # Load routes.rb if it hasn't been loaded. generated_path, extra_keys = @routes.generate_extras(, defaults) found_extras = .reject {|k, v| ! extra_keys.include? k} msg = (, "found extras <?>, not <?>", found_extras, extras) assert_equal(extras, found_extras, msg) msg = (, "The generated path <?> did not match <?>", generated_path, expected_path) assert_equal(expected_path, generated_path, msg) end |
#assert_recognizes(expected_options, path, extras = {}, message = nil) ⇒ Object
Asserts that the routing of the given path
was handled correctly and that the parsed options (given in the expected_options
hash) match path
. Basically, it asserts that Rails recognizes the route given by expected_options
.
Pass a hash in the second argument (path
) to specify the request method. This is useful for routes requiring a specific HTTP method. The hash should contain a :path with the incoming request path and a :method containing the required HTTP verb.
# assert that POSTing to /items will call the create action on ItemsController
assert_recognizes({:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post})
You can also pass in extras
with a hash containing URL parameters that would normally be in the query string. This can be used to assert that values in the query string string will end up in the params hash correctly. To test query strings you must use the extras argument, appending the query string on the path directly will not work. For example:
# assert that a path of '/items/list/1?view=print' returns the correct options
assert_recognizes({:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" })
The message
parameter allows you to pass in an error message that is displayed upon failure.
Examples
# Check the default route (i.e., the index action)
assert_recognizes({:controller => 'items', :action => 'index'}, 'items')
# Test a specific action
assert_recognizes({:controller => 'items', :action => 'list'}, 'items/list')
# Test an action with a parameter
assert_recognizes({:controller => 'items', :action => 'destroy', :id => '1'}, 'items/destroy/1')
# Test a custom route
assert_recognizes({:controller => 'items', :action => 'show', :id => '1'}, 'view/item1')
40 41 42 43 44 45 46 47 48 49 50 |
# File 'actionpack/lib/action_dispatch/testing/assertions/routing.rb', line 40 def assert_recognizes(, path, extras={}, =nil) request = recognized_request_for(path) = .clone extras.each_key { |key| .delete key } unless extras.nil? .stringify_keys! msg = (, "The recognized options <?> did not match <?>, difference: <?>", request.path_parameters, , .diff(request.path_parameters)) assert_equal(, request.path_parameters, msg) end |
#assert_routing(path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that path and options match both ways; in other words, it verifies that path
generates options
and then that options
generates path
. This essentially combines assert_recognizes
and assert_generates
into one step.
The extras
hash allows you to specify options that would normally be provided as a query string to the action. The message
parameter allows you to specify a custom error message to display upon failure.
Examples
# Assert a basic route: a controller with the default action (index)
assert_routing '/home', :controller => 'home', :action => 'index'
# Test a route generated with a specific controller, action, and parameter (id)
assert_routing '/entries/show/23', :controller => 'entries', :action => 'show', :id => 23
# Assert a basic route (controller + default action), with an error message if it fails
assert_routing '/store', { :controller => 'store', :action => 'index' }, {}, {}, 'Route for store index not generated properly'
# Tests a route, providing a defaults hash
assert_routing 'controller/action/9', {:id => "9", :item => "square"}, {:controller => "controller", :action => "action"}, {}, {:item => "square"}
# Tests a route with a HTTP method
assert_routing({ :method => 'put', :path => '/product/321' }, { :controller => "product", :action => "update", :id => "321" })
116 117 118 119 120 121 122 123 124 125 126 |
# File 'actionpack/lib/action_dispatch/testing/assertions/routing.rb', line 116 def assert_routing(path, , defaults={}, extras={}, =nil) assert_recognizes(, path, extras, ) controller, default_controller = [:controller], defaults[:controller] if controller && controller.include?(?/) && default_controller && default_controller.include?(?/) [:controller] = "/#{controller}" end = .dup.delete_if{ |k,v| defaults.key?(k) } assert_generates(path.is_a?(Hash) ? path[:path] : path, , defaults, extras, ) end |
#with_routing ⇒ Object
A helper to make it easier to test different route configurations. This method temporarily replaces @routes with a new RouteSet instance.
The new instance is yielded to the passed block. Typically the block will create some routes using map.draw { map.connect ... }
:
with_routing do |set|
set.draw do |map|
map.connect ':controller/:action/:id'
assert_equal(
['/content/10/show', {}],
map.generate(:controller => 'content', :id => 10, :action => 'show')
end
end
end
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'actionpack/lib/action_dispatch/testing/assertions/routing.rb', line 145 def with_routing old_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new if defined?(@controller) && @controller old_controller, @controller = @controller, @controller.clone _routes = @routes # Unfortunately, there is currently an abstraction leak between AC::Base # and AV::Base which requires having the URL helpers in both AC and AV. # To do this safely at runtime for tests, we need to bump up the helper serial # to that the old AV subclass isn't cached. # # TODO: Make this unnecessary @controller.singleton_class.send(:include, _routes.url_helpers) @controller.view_context_class = Class.new(@controller.view_context_class) do include _routes.url_helpers end end yield @routes ensure @routes = old_routes if defined?(@controller) && @controller @controller = old_controller end end |