module ActionDispatch::Assertions::RoutingAssertions
def assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil)
# Asserts that the generated route gives us our custom route
assert_generates "/items/list/1", { controller: "items", action: "list", id: "1" }
# Tests the generation of a route with a parameter
assert_generates "/items/list", controller: "items", action: "list"
# Tests that the list action is properly routed
assert_generates "/items", controller: "items", action: "index"
# Asserts that the default action is generated for a route with no action
The +defaults+ parameter is unused.
a query string. The +message+ parameter allows you to specify a custom error message for assertion failures.
The +extras+ parameter is used to tell the request the names and values of additional request parameters that would be in
Asserts that the provided options can be used to generate the provided path. This is the inverse of +assert_recognizes+.
def assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil) if expected_path.include?("://") fail_on(URI::InvalidURIError, message) do uri = URI.parse(expected_path) expected_path = uri.path.to_s.empty? ? "/" : uri.path end else expected_path = "/#{expected_path}" unless expected_path.start_with?("/") end options = options.clone generated_path, query_string_keys = @routes.generate_extras(options, defaults) found_extras = options.reject { |k, _| ! query_string_keys.include? k } msg = message || sprintf("found extras <%s>, not <%s>", found_extras, extras) assert_equal(extras, found_extras, msg) msg = message || sprintf("The generated path <%s> did not match <%s>", generated_path, expected_path) assert_equal(expected_path, generated_path, msg) end