Module: ActionController::Assertions::RoutingAssertions
- Defined in:
- lib/action_controller/assertions/routing_assertions.rb
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 match.
-
#assert_routing(path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that path and options match both ways; in other words, the URL generated from options is the same as path, and also that the options recognized from path are the same as options.
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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/action_controller/assertions/routing_assertions.rb', line 37 def assert_generates(expected_path, , defaults={}, extras = {}, =nil) clean_backtrace do expected_path = "/#{expected_path}" unless expected_path[0] == ?/ # Load routes.rb if it hasn't been loaded. ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(, defaults) found_extras = .reject {|k, v| ! extra_keys.include? k} msg = (, "found extras <?>, not <?>", found_extras, extras) assert_block(msg) { found_extras == extras } msg = (, "The generated path <?> did not match <?>", generated_path, expected_path) assert_block(msg) { expected_path == generated_path } end 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 match.
assert_recognizes({:controller => 'items', :action => 'index'}, 'items')
Pass a hash in the second argument to specify the request method. This is useful for routes requiring a specific method.
assert_recognizes({:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post})
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/action_controller/assertions/routing_assertions.rb', line 13 def assert_recognizes(, path, extras={}, =nil) if path.is_a? Hash request_method = path[:method] path = path[:path] else request_method = nil end clean_backtrace do ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? request = recognized_request_for(path, request_method) = .clone extras.each_key { |key| .delete key } unless extras.nil? .stringify_keys! routing_diff = .diff(request.path_parameters) msg = (, "The recognized options <?> did not match <?>, difference: <?>", request.path_parameters, , .diff(request.path_parameters)) assert_block(msg) { request.path_parameters == } end end |
#assert_routing(path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that path and options match both ways; in other words, the URL generated from options is the same as path, and also that the options recognized from path are the same as options
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/action_controller/assertions/routing_assertions.rb', line 57 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 assert_generates(path, , defaults, extras, ) end |