Testing Routes

Although the basics of Rails routing are simple, the desire to customize Rails’ response to URLs can lead to confusion about exactly what your application is going to do when converting between a URL and a Rails action. Rails provides a way to specify route behavior in a test.

Routing tests are not typically part of my TDD process—usually my integration test implicitly covers the routing. That said, sometimes routing gets complicated and has some logic of its own (especially if you’re trying to replicate an existing URL scheme), so it’s nice to have this as part of your test suite.

RSpec-Rails puts route tests in the spec/routing directory. The primary matcher that RSpec-Rails uses for route testing is route_to. Here’s ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.