Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test routes in a Rails 3.1 mountable engine

Tags:

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.

like image 890
Cameron Pope Avatar asked Oct 07 '11 18:10

Cameron Pope


People also ask

What is Mount in Rails routes?

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.

What is Ruby on Rails engine?

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.


2 Answers

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

like image 131
andrerobot Avatar answered Nov 24 '22 07:11

andrerobot


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.

like image 22
Steven Anderson Avatar answered Nov 24 '22 06:11

Steven Anderson