instance method assert_recognizes

Ruby on Rails 3.1.12

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

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

Parameters

expected_options req
path req
extras opt = {}
message opt = nil
Source
# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 40
      def assert_recognizes(expected_options, path, extras={}, message=nil)
        request = recognized_request_for(path)

        expected_options = expected_options.clone
        extras.each_key { |key| expected_options.delete key } unless extras.nil?

        expected_options.stringify_keys!
        msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>",
            request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
        assert_equal(expected_options, request.path_parameters, msg)
      end

Defined in actionpack/lib/action_dispatch/testing/assertions/routing.rb line 40 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Assertions::RoutingAssertions

Type at least 2 characters to search.

↑↓ navigate · open · esc close