5

I have multiple Rails engines in my Rails 4 beta1 application. I'm installed rspec-rails gem to every engines. And I created my engine following command:

rails plugin new store_frontend --dummy-path=spec/dummy -d postgresql --skip-test-unit --mountable

In my engine's dummy application I configured database and routes. Here is example routes.rb file:

Rails.application.routes.draw do

  mount StoreFrontend::Engine => "/store"
end

When I run rspec inside first engine I get following errors:

  1) StoreAdmin::DashboardController GET 'index' returns http success
     Failure/Error: get 'index'
     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"store_admin/dashboard"}
     # ./spec/controllers/store_admin/dashboard_controller_spec.rb:8:in `block (3 levels) in <module:StoreAdmin>'

And here is my controller test /It's generated from Rails/:

require 'spec_helper'

module StoreFrontend
  describe HomeController do

    describe "GET 'index'" do
      it "returns http success" do
        get 'index'
        response.should be_success
      end
    end

  end
end

It seems like controller test is not working. I have model tests and it's working fine. Any idea?

UPDATE 1: My application structure:

bin/
config/
db/
lib/
log/
public/
tmp/
engine1/
engine2/
engine3/
Zeck
  • 6,433
  • 21
  • 71
  • 111

3 Answers3

15

The solution is very simple. Add use_route to your controller test. Here is the example.

module StoreFrontend
  describe HomeController do

    describe "GET 'index'" do
      it "returns http success" do
        get 'index', use_route: 'store_frontend' # in my case
        response.should be_success
      end
    end

  end
end
Zeck
  • 6,433
  • 21
  • 71
  • 111
  • How does changing `StoreFronted` fix `StoreAdmin::DashboardController` ? How does this fix `No route matches {:action=>"index", :controller=>"store_admin/dashboard"}`? – Old Pro May 04 '13 at 22:46
  • Check this out: http://stackoverflow.com/questions/5200654/how-do-i-write-a-rails-3-1-engine-controller-test-in-rspec – Karan Aug 02 '13 at 19:34
  • Thanks, 'use_route' resolved the UrlGenerationError issue for me. – TK-421 Dec 04 '14 at 19:33
  • Thanks for this answer. I didn't have exactly the same problem described in the original question, but your answer applies. I have a series of 'Lookup' models derived from a base model using STI (Single Table Inheritance) and a single controller for all Lookup derived types. RSpec was spitting out the same UrlGenerationError. We ran into a similar – Yoopergeek Apr 21 '16 at 03:23
3

The configuration and spec you show are for StoreFrontend but the error is for StoreAdmin::DashboardController. So it seems like you are just confused about which engine you are testing and/or which engine is failing.

Of course the simple solution is to create the missing route {:action=>"index", :controller=>"store_admin/dashboard"}

Old Pro
  • 24,624
  • 7
  • 58
  • 106
2

In order to get the routing correct when testing Rails engine controllers with Rspec, I typically add the following code to my spec_helper.rb:

RSpec.configure do |config|
  config.before(:each, :type => :controller) { @routes = YourEngineName::Engine.routes }
  config.before(:each, :type => :routing)    { @routes = YourEngineName::Engine.routes }
end
jcypret
  • 1,275
  • 1
  • 10
  • 7