Gavin Morrice

is a web and iOS developer from Edinburgh, Scotland.

more about me ยป

How to test routes in a Rails 3.1 mountable engine

This problem has been eluding me for a few weeks...

While working on Blogit I wanted to write specs for the routes but kept hitting ActionController::RoutingError with every approach I tried.

Finally I discovered this solution:

# spec/routing/post_routing_spec.rb

...

before do
  @routes = Blogit::Engine.routes
end

it "routes /posts/page/:page to posts#index with page param" do
  { get: "posts/page/2" }.should route_to({
    controller: "blogit/posts",
    action: "index",
    page: "2"
  }
end

Setting the @routes variable to the Blogit Engine's routes seemed to do the trick nicely!

There's something really dirty and "hacky" about this approach though - there surely is a better way? If anyone knows, please leave a comment below.