1 module ActionController
3 module RoutingAssertions
4 # Asserts that the routing of the given path was handled correctly and that the parsed options match.
6 # assert_recognizes({:controller => 'items', :action => 'index'}, 'items') # check the default action
7 # assert_recognizes({:controller => 'items', :action => 'list'}, 'items/list') # check a specific action
8 # assert_recognizes({:controller => 'items', :action => 'list', :id => '1'}, 'items/list/1') # check an action with a parameter
10 # Pass a hash in the second argument to specify the request method. This is useful for routes
11 # requiring a specific HTTP method. The hash should contain a :path with the incoming request path
12 # and a :method containing the required HTTP verb.
14 # # assert that POSTing to /items will call the create action on ItemsController
15 # assert_recognizes({:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post})
17 # You can also pass in "extras" with a hash containing URL parameters that would normally be in the query string. This can be used
18 # 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
19 # extras argument, appending the query string on the path directly will not work. For example:
21 # # assert that a path of '/items/list/1?view=print' returns the correct options
22 # assert_recognizes({:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" })
23 def assert_recognizes(expected_options, path, extras={}, message=nil)
25 request_method = path[:method]
32 ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
33 request = recognized_request_for(path, request_method)
35 expected_options = expected_options.clone
36 extras.each_key { |key| expected_options.delete key } unless extras.nil?
38 expected_options.stringify_keys!
39 routing_diff = expected_options.diff(request.path_parameters)
40 msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>",
41 request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
42 assert_block(msg) { request.path_parameters == expected_options }
46 # Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes.
49 # assert_generates("/items", :controller => "items", :action => "index")
50 # assert_generates("/items/list", :controller => "items", :action => "list")
51 # assert_generates("/items/list/1", { :controller => "items", :action => "list", :id => "1" })
52 def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
54 expected_path = "/#{expected_path}" unless expected_path[0] == ?/
55 # Load routes.rb if it hasn't been loaded.
56 ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
58 generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(options, defaults)
59 found_extras = options.reject {|k, v| ! extra_keys.include? k}
61 msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
62 assert_block(msg) { found_extras == extras }
64 msg = build_message(message, "The generated path <?> did not match <?>", generated_path,
66 assert_block(msg) { expected_path == generated_path }
70 # Asserts that path and options match both ways; in other words, the URL generated from
71 # options is the same as path, and also that the options recognized from path are the same as options. This
72 # essentially combines assert_recognizes and assert_generates into one step.
73 def assert_routing(path, options, defaults={}, extras={}, message=nil)
74 assert_recognizes(options, path, extras, message)
76 controller, default_controller = options[:controller], defaults[:controller]
77 if controller && controller.include?(?/) && default_controller && default_controller.include?(?/)
78 options[:controller] = "/#{controller}"
81 assert_generates(path, options, defaults, extras, message)
85 # Recognizes the route for a given path.
86 def recognized_request_for(path, request_method = nil)
87 path = "/#{path}" unless path.first == '/'
89 # Assume given controller
90 request = ActionController::TestRequest.new({}, {}, nil)
91 request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
94 ActionController::Routing::Routes.recognize(request)