In a Rails test, what do assert_recognizes, assert_generates and assert_routing each check about a route, and when would you use each?
answer
- path to params, params to path
- assert_routing checks both ways
- pass method: in a path hash
- query params go in extras
- values compare as strings
basics
~10 sassert_recognizes checks that a path (and verb) routes to the expected controller, action and params; assert_generates checks that those options build the expected path; assert_routing checks both directions at once.
solid answer
~40 sAll three come from `ActionDispatch::Assertions` and work in `ActionDispatch::IntegrationTest` and `ActionController::TestCase`. `assert_recognizes({controller: "bikes", action: "nearby"}, "/bikes/nearby")` runs the path through the router and compares the resulting path params; for another verb pass `{path: "/bikes/1", method: :patch}`. `assert_generates("/bikes/nearby", controller: "bikes", action: "nearby")` goes the other way, building a path from options. `assert_routing` does both, which suits a URL you want to keep stable in both directions. Query parameters go in the `extras` argument, not on the path, and params come back as strings, so write `id: "7"`. Use `assert_recognizes` alone when generation would pick a different route on purpose, `assert_generates` alone when several paths lead to the same action, and remember that redirect routes are invisible to recognition.
code
ruby · 15 linesclass BikeRoutesTest < ActionDispatch::IntegrationTest
test "nearby is not shadowed by show" do
assert_recognizes({ controller: "bikes", action: "nearby" }, "/bikes/nearby")
end
test "bike update route is stable both ways" do
assert_routing({ path: "/bikes/7", method: :patch },
{ controller: "bikes", action: "update", id: "7" })
end
test "search params reach the action" do
assert_recognizes({ controller: "bikes", action: "index", dock: "central" },
"/bikes", { dock: "central" })
end
endgo deeper
Recall that recognition goes from path to params, generation from options to path, and assert_routing checks both.
Use method: hashes for other verbs, extras for query values and string ids, and know which test classes include the assertions.
Spend routing tests on ordering-sensitive and contract URLs, and test redirect routes and Rack mounts with real requests instead.
Decide which URLs are contracts worth pinning both ways, and keep the routing test suite small enough to stay meaningful.
## Two directions of a route A route works in two directions: - **Recognition**: an incoming path and verb become a controller, an action and path params. - **Generation**: a set of options becomes a path, which is what helpers and `url_for` do. Rails ships one assertion per direction plus one for both. They live in `ActionDispatch::Assertions::RoutingAssertions`, which is included in `ActionDispatch::IntegrationTest` and `ActionController::TestCase`. | Assertion | Checks | Signature (simplified) | |---|---|---| | `assert_recognizes` | path → params | `assert_recognizes(expected_options, path, extras = {}, message = nil)` | | `assert_generates` | options → path | `assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil)` | | `assert_routing` | both | `assert_routing(path, options, defaults = {}, extras = {}, message = nil)` | ## assert_recognizes ```ruby assert_recognizes({ controller: "bikes", action: "nearby" }, "/bikes/nearby") assert_recognizes({ controller: "bikes", action: "update", id: "7" }, { path: "/bikes/7", method: :patch }) ``` - The path is recognized as a **GET** unless you pass a hash with `method:`. - The recognized params are compared with the expected hash exactly, so every segment must be listed and values are **strings** (`id: "7"`). - A query string appended to the path is not part of recognition; to assert that query values land in params, pass them as **`extras`**. - If nothing matches, the assertion fails with the router's "No route matches" message. This is the assertion that would have caught the shadowed `/bikes/nearby` route: with the bug present, recognition returns `show` with `id: "nearby"` and the comparison fails. ## assert_generates ```ruby assert_generates "/bikes/nearby", controller: "bikes", action: "nearby" assert_generates "/bikes/7/edit", controller: "bikes", action: "edit", id: "7" ``` It builds a path from the options the way `url_for` would and compares it with the expected path. Use it when several paths reach one action and you care which one links produce, such as a legacy alias that should still be recognized but never generated. ## assert_routing ```ruby assert_routing({ path: "/bikes/7", method: :patch }, { controller: "bikes", action: "update", id: "7" }) ``` It runs `assert_recognizes` and then `assert_generates` with the same pair. It is the natural choice for a URL that must stay stable in both directions, such as a public share link or a webhook endpoint registered with a partner. ## Picking one 1. A URL must reach the right action: `assert_recognizes`. 2. Helpers must keep producing a given URL: `assert_generates`. 3. Both, for contract-like URLs: `assert_routing`. 4. The URL is a `redirect(...)` route or a mounted Rack app: none of these. Recognition only reports controller routes, so request the URL in an integration test and assert the response instead. ## Limits - **They test the route table, not the action.** A passing `assert_recognizes` says nothing about whether `nearby` renders. - **Request constraints** are evaluated against a test request, so a subdomain route needs a full URL as the path, such as `"http://admin.example.com/bikes"`. - **Do not assert every resource route.** `resources :bikes` is Rails' own code; spend routing tests on custom routes, ordering-sensitive ones and URLs other systems depend on. ## Routing assertions or request tests? | Need | Better tool | |---|---| | Prove a path reaches an action, quickly and without the database | routing assertion | | Prove the action responds correctly | integration request (`get "/bikes/nearby"`) and response assertions | | A redirect route or a mounted Rack app | integration request | | A URL another system depends on | `assert_routing` plus a request test | Routing assertions are cheap because nothing is dispatched: no action runs, no view renders, no queries are made. That makes them a good fit for guarding route order, and a poor fit for anything about behaviour. ## Temporary routes For code that depends on routes that should not exist in the app (a library helper, say), `with_routing` swaps in a temporary route set for one test: ```ruby with_routing do |set| set.draw { get "tmp/ping", to: "bikes#index" } assert_recognizes({ controller: "bikes", action: "index" }, "/tmp/ping") end ``` The original routes are restored when the block ends.
- In a Rails test, why does assert_recognizes fail for a get "old-docks", to: redirect("/docks") route?Recognition only reports routes that dispatch to a controller or an engine. A redirect endpoint is skipped, so the assertion finds no matching controller route and fails with "No route matches" (or matches a later route). Test redirect routes with an integration request and `assert_redirected_to` instead.
- How do you check in a Rails routing assertion that ?dock=central ends up in params?Pass it as the `extras` argument: `assert_recognizes({controller: "bikes", action: "index", dock: "central"}, "/bikes", {dock: "central"})`. Appending `?dock=central` to the path does not work, because recognition only reads the path; the API documentation says to use extras for query values.
- assert_routing only checks that the path is recognized
- Appending ?dock=central to the path tests query params
- assert_recognizes defaults to the verb of the route it finds
- Routing assertions also run the controller action
- Every route from resources should get its own routing assertion