4

我的Rails 4 beta1应用程序中有多个Rails引擎。我为每个引擎安装了rspec-rails宝石。我建立了我的引擎以下命令:多个Rails引擎rspec控制器测试不起作用

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

在我的引擎的虚拟应用程序,我配置的数据库和路线。下面是例子的routes.rb文件:

Rails.application.routes.draw do 

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

当我第一次发动机内部rspec的运行,我得到以下错误:

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>' 

,这里是我的控制器测试/它是从轨道/生成:

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 

看起来控制器测试不起作用。我有模型测试,它工作正常。任何想法?

更新1: 我的应用程序结构:

bin/ 
config/ 
db/ 
lib/ 
log/ 
public/ 
tmp/ 
engine1/ 
engine2/ 
engine3/ 
+1

规范展示和规范的结果不涉及相同的代码 – apneadiving 2013-04-26 08:15:21

回答

14

的解决方案是非常简单的。将use_route添加到您的控制器测试。这里是例子。

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 
+0

如何更改'StoreFronted'修复'StoreAdmin :: DashboardController'?这是怎么解决的?没有路由匹配{:action =>“index”,:controller =>“store_admin/dashboard”}'? – 2013-05-04 22:46:55

+0

看看这个:http://stackoverflow.com/questions/5200654/how-do-i-write-a-rails-3-1-engine-controller-test-in-rspec – Karan 2013-08-02 19:34:48

+0

谢谢,'use_route'解决了UrlGenerationError问题对我来说。 – 2014-12-04 19:33:14

3

配置及规格告诉你是StoreFrontend但误差为StoreAdmin::DashboardController。因此,您似乎对您正在测试哪个引擎和/或哪个引擎失败感到困惑。

当然简单的解决方案是创建缺少的路线{:action=>"index", :controller=>"store_admin/dashboard"}

2

为了得到路由正确使用RSpec测试Rails的引擎控制器的时候,我通常是下面的代码添加到我的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