module ActionDispatch::Assertions::RoutingAssertions
def assert_recognizes(expected_options, path, extras = {}, msg = nil)
# Test a custom route
assert_recognizes({controller: 'items', action: 'destroy', id: '1'}, 'items/destroy/1')
# Test an action with a parameter
assert_recognizes({controller: 'items', action: 'list'}, 'items/list')
# Test a specific action
assert_recognizes({controller: 'items', action: 'index'}, 'items')
# Check the default route (i.e., the index action)
displayed upon failure.
The `message` parameter allows you to pass in an error message that is
assert_recognizes({controller: 'items', action: 'list', id: '1', view: 'print'}, 'items/list/1', { view: "print" })
# Asserts that a path of '/items/list/1?view=print' returns the correct options
path directly will not work. For example:
you must use the extras argument because appending the query string on the
query string will end up in the params hash correctly. To test query strings
normally be in the query string. This can be used to assert that values in the
You can also pass in `extras` with a hash containing URL parameters that would
assert_recognizes({controller: 'items', action: 'create'}, {path: 'items', method: :post})
# Asserts that POSTing to /items will call the create action on ItemsController
the required HTTP verb.
contain a `:path` with the incoming request path and a `:method` containing
This is useful for routes requiring a specific HTTP method. The hash should
Pass a hash in the second argument (`path`) to specify the request method.
`expected_options`.
Basically, it asserts that Rails recognizes the route given by
the parsed options (given in the `expected_options` hash) match `path`.
Asserts that the routing of the given `path` was handled correctly and that
def assert_recognizes(expected_options, path, extras = {}, msg = nil) if path.is_a?(Hash) && path[:method].to_s == "all" [:get, :post, :put, :delete].each do |method| assert_recognizes(expected_options, path.merge(method: method), extras, msg) end else request = recognized_request_for(path, extras, msg) expected_options = expected_options.clone expected_options.stringify_keys! msg = message(msg, "") { sprintf("The recognized options <%s> did not match <%s>, difference:", request.path_parameters, expected_options) } assert_equal(expected_options, request.path_parameters, msg) end end