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

Methods
Public Instance methods
assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)

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 actionpack/lib/action_controller/assertions/routing_assertions.rb, line 80
80:       def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
81:         clean_backtrace do
82:           expected_path = "/#{expected_path}" unless expected_path[0] == ?/
83:           # Load routes.rb if it hasn't been loaded.
84:           ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
85: 
86:           generated_path, extra_keys = ActionController::Routing::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
96:       end
assert_recognizes(expected_options, path, extras={}, message=nil)

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 actionpack/lib/action_controller/assertions/routing_assertions.rb, line 39
39:       def assert_recognizes(expected_options, path, extras={}, message=nil)
40:         if path.is_a? Hash
41:           request_method = path[:method]
42:           path           = path[:path]
43:         else
44:           request_method = nil
45:         end
46: 
47:         clean_backtrace do
48:           ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
49:           request = recognized_request_for(path, request_method)
50: 
51:           expected_options = expected_options.clone
52:           extras.each_key { |key| expected_options.delete key } unless extras.nil?
53: 
54:           expected_options.stringify_keys!
55:           routing_diff = expected_options.diff(request.path_parameters)
56:           msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>",
57:               request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
58:           assert_block(msg) { request.path_parameters == expected_options }
59:         end
60:       end
assert_routing(path, options, defaults={}, extras={}, message=nil)

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 actionpack/lib/action_controller/assertions/routing_assertions.rb, line 120
120:       def assert_routing(path, options, defaults={}, extras={}, message=nil)
121:         assert_recognizes(options, path, extras, message)
122: 
123:         controller, default_controller = options[:controller], defaults[:controller]
124:         if controller && controller.include?(?/) && default_controller && default_controller.include?(?/)
125:           options[:controller] = "/#{controller}"
126:         end
127: 
128:         assert_generates(path.is_a?(Hash) ? path[:path] : path, options, defaults, extras, message)
129:       end