Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails engine: rake routes show its routes but on rspec execution "No route matches" is thrown

I'm trying to test a controller that is inside an engine my application is using. The spec is not within the engine, but in the application itself (I tried to test within the engine but also had problems).

My engine has the following routes.rb:

Revision::Engine.routes.draw do
  resources :steps, only: [] do
    collection { get :first }
  end
end

The engine is mounted on the application routes.rb normally:

mount Revision::Engine => "revision"

When I run rake routes, at the last lines I get:

Routes for Revision::Engine:
first_steps GET /steps/first(.:format) revision/steps#first
       root     /                       revision/steps#first

On my engine's controller (lib/revision/app/controllers/revision/steps_controller.rb), I have:

module Revision
  class StepsController < ApplicationController
    def first
    end
  end
end

On Rspec, I test this controller with:

require 'spec_helper'
describe Revision::StepsController do
  it "should work" do
    get :first
    response.should be_success
  end
end

Then when I run this spec, I get:

ActionController::RoutingError:
   No route matches {:controller=>"revision/steps", :action=>"first"}

To be sure that the route doesn't really exist, I added this to the spec:

before do
  puts @routes.set.to_a.map(&:defaults)
end

And the result is this:

[...]
{:action=>"show", :controller=>"devise/unlocks"}
{:action=>"revision"}

It has only the :action parameter.

What may be wrong?

like image 616
Carlos Eduardo da Fonseca Avatar asked Jan 18 '13 15:01

Carlos Eduardo da Fonseca


1 Answers

When you're trying to test an engine's controllers, you need to specify what route set you want the controller test to use, otherwise it will run it against the main app's. To do that, pass use_route: :engine_name to the get method.

require 'spec_helper'
describe Revision::StepsController do
  it "should work" do
    get :first, use_route: :revision # <- this is how you do it
    response.should be_success
  end
end
like image 170
midu Avatar answered Oct 19 '22 22:10

midu