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.
Mount within the Rails routes does the equivalent of a Unix mount . It actually tells the app that another application (usually a Rack application) exists on that location. It is used mostly for Rails Engines.
This application is used as a mounting point for the engine, to make testing the engine extremely simple. You may extend this application by generating controllers, models, or views from within the directory, and then use those to test your engine.
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
Try adding a before block with the following:
before(:each) { @routes = Testy::Engine.routes }
That worked for me, as the routing specs use that top level instance variable to test their routes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With