Class Index [+]

Quicksearch

ActionDispatch::Assertions::RoutingAssertions

Suite of assertions to test routes generated by Rails and the handling of requests made to them.

Public Instance Methods

assert_generates(expected_path, options, defaults={}, extras = {}, message=nil) click to toggle source

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" }
    # File lib/action_dispatch/testing/assertions/routing.rb, line 73
73:       def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
74:         if expected_path =~ %{://}
75:           begin
76:             uri = URI.parse(expected_path)
77:             expected_path = uri.path.to_s.empty? ? "/" : uri.path
78:           rescue URI::InvalidURIError => e
79:             raise ActionController::RoutingError, e.message
80:           end
81:         else
82:           expected_path = "/#{expected_path}" unless expected_path.first == '/'
83:         end
84:         # Load routes.rb if it hasn't been loaded.
85: 
86:         generated_path, extra_keys = @routes.generate_extras(options, defaults)
87:         found_extras = options.reject {|k, v| ! extra_keys.include? k}
88: 
89:         msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
90:         assert_block(msg) { found_extras == extras }
91: 
92:         msg = build_message(message, "The generated path <?> did not match <?>", generated_path,
93:             expected_path)
94:         assert_block(msg) { expected_path == generated_path }
95:       end
assert_recognizes(expected_options, path, extras={}, message=nil) click to toggle source

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')

  # Check a Simply RESTful generated route
  assert_recognizes list_items_url, 'items/list'
    # File lib/action_dispatch/testing/assertions/routing.rb, line 43
43:       def assert_recognizes(expected_options, path, extras={}, message=nil)
44:         request = recognized_request_for(path)
45: 
46:         expected_options = expected_options.clone
47:         extras.each_key { |key| expected_options.delete key } unless extras.nil?
48: 
49:         expected_options.stringify_keys!
50:         msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>",
51:             request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
52:         assert_block(msg) { request.path_parameters == expected_options }
53:       end
assert_routing(path, options, defaults={}, extras={}, message=nil) click to toggle source

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" })
     # File lib/action_dispatch/testing/assertions/routing.rb, line 119
119:       def assert_routing(path, options, defaults={}, extras={}, message=nil)
120:         assert_recognizes(options, path, extras, message)
121: 
122:         controller, default_controller = options[:controller], defaults[:controller]
123:         if controller && controller.include?(//) && default_controller && default_controller.include?(//)
124:           options[:controller] = "/#{controller}"
125:         end
126: 
127:         assert_generates(path.is_a?(Hash) ? path[:path] : path, options, defaults, extras, message)
128:       end
method_missing(selector, *args, &block) click to toggle source

ROUTES TODO: These assertions should really work in an integration context

     # File lib/action_dispatch/testing/assertions/routing.rb, line 173
173:       def method_missing(selector, *args, &block)
174:         if @controller && @routes && @routes.named_routes.helpers.include?(selector)
175:           @controller.send(selector, *args, &block)
176:         else
177:           super
178:         end
179:       end
with_routing() click to toggle source

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
     # File lib/action_dispatch/testing/assertions/routing.rb, line 147
147:       def with_routing
148:         old_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
149:         old_controller, @controller = @controller, @controller.clone if @controller
150:         _routes = @routes
151: 
152:         # Unfortunately, there is currently an abstraction leak between AC::Base
153:         # and AV::Base which requires having the URL helpers in both AC and AV.
154:         # To do this safely at runtime for tests, we need to bump up the helper serial
155:         # to that the old AV subclass isn't cached.
156:         #
157:         # TODO: Make this unnecessary
158:         if @controller
159:           @controller.singleton_class.send(:include, _routes.url_helpers)
160:           @controller.view_context_class = Class.new(@controller.view_context_class) do
161:             include _routes.url_helpers
162:           end
163:         end
164:         yield @routes
165:       ensure
166:         @routes = old_routes
167:         if @controller
168:           @controller = old_controller
169:         end
170:       end

Private Instance Methods

recognized_request_for(path) click to toggle source

Recognizes the route for a given path.

     # File lib/action_dispatch/testing/assertions/routing.rb, line 183
183:         def recognized_request_for(path)
184:           if path.is_a?(Hash)
185:             method = path[:method]
186:             path   = path[:path]
187:           else
188:             method = :get
189:           end
190: 
191:           # Assume given controller
192:           request = ActionController::TestRequest.new
193: 
194:           if path =~ %{://}
195:             begin
196:               uri = URI.parse(path)
197:               request.env["rack.url_scheme"] = uri.scheme || "http"
198:               request.host = uri.host if uri.host
199:               request.port = uri.port if uri.port
200:               request.path = uri.path.to_s.empty? ? "/" : uri.path
201:             rescue URI::InvalidURIError => e
202:               raise ActionController::RoutingError, e.message
203:             end
204:           else
205:             path = "/#{path}" unless path.first == "/"
206:             request.path = path
207:           end
208: 
209:           request.request_method = method if method
210: 
211:           params = @routes.recognize_path(path, { :method => method })
212:           request.path_parameters = params.with_indifferent_access
213: 
214:           request
215:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.