Testing routes in isolated Rails Engines with RSpec
One problem that you can run into when implementing a Rails Engine is hit or miss third party library support. Even when a library does support engines (i.e. RSpec), because few people code in the context of them, documentation and online support can be few and far between. When trying to test my mounted and isolated engine’s routes I quickly realized RSpec would produce failures because it was loading my main app’s routes into the tests instead of my engine’s. It seemed like it should be an easy fix but none of the solutions I found online worked.
Only after a “bundle show rspec-rails” and diving into the code did I find the very easy but non-obvious hook that fixed everything. Turns out this hook was only added recently to routing tests, is being added to controller tests as we speak, and documentation is certainly light. To set the routes used in Routing Tests (and soon Controller tests) use this public API:
If you want to read up on the history of the this new feature, see these two commits:
