instance method assert_generates

Ruby on Rails 3.0.20

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_generates(expected_path, options, defaults={}, extras = {}, message=nil)

Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.

The defaults parameter is unused.

Examples

# Asserts that the default action is generated for a route with no action
assert_generates "/items", :controller => "items", :action => "index"

# Tests that the list action is properly routed
assert_generates "/items/list", :controller => "items", :action => "list"

# Tests the generation of a route with a parameter
assert_generates "/items/list/1", { :controller => "items", :action => "list", :id => "1" }

# Asserts that the generated route gives us our custom route
assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }

Parameters

expected_path req
options req
defaults opt = {}
extras opt = {}
message opt = nil
Source
# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 70
      def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
        if expected_path =~ %r{://}
          begin
            uri = URI.parse(expected_path)
            expected_path = uri.path.to_s.empty? ? "/" : uri.path
          rescue URI::InvalidURIError => e
            raise ActionController::RoutingError, e.message
          end
        else
          expected_path = "/#{expected_path}" unless expected_path.first == '/'
        end
        # Load routes.rb if it hasn't been loaded.

        generated_path, extra_keys = @routes.generate_extras(options, defaults)
        found_extras = options.reject {|k, v| ! extra_keys.include? k}

        msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
        assert_block(msg) { found_extras == extras }

        msg = build_message(message, "The generated path <?> did not match <?>", generated_path,
            expected_path)
        assert_block(msg) { expected_path == generated_path }
      end

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