简体   繁体   中英

Multiple Rails engine rspec controller test not work

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/

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

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"}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM