Ruby-on-rails – How to test routes in a Rails 3.1 mountable engine

rspec2ruby-on-railsruby-on-rails-3

I am trying to write some routing specs for a mountable rails 3.1 engine. I have working model and controller specs, but I cannot figure out how to specify routes.

For a sample engine, 'testy', every approach I try ends with the same error:

 ActionController::RoutingError:
   No route matches "/testy"

I've tried both Rspec and Test::Unit syntax (spec/routing/index_routing_spec.rb):

describe "test controller routing" do
  it "Routs the root to the test controller's index action" do
    { :get => '/testy/' }.should route_to(:controller => 'test', :action => 'index')
  end

  it "tries the same thing using Test::Unit syntax" do
    assert_routing({:method => :get, :path => '/testy/', :use_route => :testy}, {:controller => 'test', :action => 'index'})
  end
end

I've laid out the routes correctly (config/routes.rb):

Testy::Engine.routes.draw do
  root :to => 'test#index'
end

And mounted them in the dummy app (spec/dummy/config/routes.rb):

Rails.application.routes.draw do
  mount Testy::Engine => "/testy"
end

And running rails server and requesting http://localhost:3000/testy/ works just fine.

Am I missing anything obvious, or is this just not properly baked into the framework yet?

Update: As @andrerobot points out, the rspec folks have fixed this issue in version 2.14, so I've changed my accepted answer accordingly.

Best Answer

Since RSpec 2.14 you can use the following:

describe "test controller routing" do
  routes { Testy::Engine.routes }
  # ...
end

Source: https://github.com/rspec/rspec-rails/pull/668

Related Topic