instance method assert_routing

Ruby on Rails 7.2.3

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_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.

# Asserts 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

# Asserts 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 an HTTP method
assert_routing({ method: 'put', path: '/product/321' }, { controller: "product", action: "update", id: "321" })

Parameters

path req
options req
defaults opt = {}
extras opt = {}
message opt = nil
Source
# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 248
      def assert_routing(path, options, defaults = {}, extras = {}, message = nil)
        assert_recognizes(options, path, extras, message)

        controller, default_controller = options[:controller], defaults[:controller]
        if controller && controller.include?(?/) && default_controller && default_controller.include?(?/)
          options[:controller] = "/#{controller}"
        end

        generate_options = options.dup.delete_if { |k, _| defaults.key?(k) }
        assert_generates(path.is_a?(Hash) ? path[:path] : path, generate_options, defaults, extras, message)
      end

Defined in actionpack/lib/action_dispatch/testing/assertions/routing.rb line 248 · 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