Testing Routes
Learn how to test the routes and route testing matchers.
We'll cover the following
Route testing
Let’s start the tour with a part of the Rails layer that’s not commonly tested in isolation but where testing can sometimes be valuable. Although Rails routing basics are simple, the desire to customize Rails’ response to URLs can lead to confusion about exactly what our application is going to do when converting between a URL and a Rails action. Rails provide a way to specify route behavior in a test.
Routing tests are not typically part of the TDD process. Usually, integration tests implicitly cover the routing. That said, sometimes routing gets complicated and has some logic of its own (especially if we’re trying to replicate an existing URL scheme), so it’s nice to have this as part of our test suite.
Route testing matchers
RSpec-Rails puts routing tests in the spec/routing
directory. The primary matcher that RSpec-Rails uses for route testing is route_to
. Here’s a sample test that includes all seven default RESTful routes for the project
resource:
Get hands-on with 1400+ tech skills courses.