简体   繁体   English

多个Rails引擎rspec控制器测试不起作用

[英]Multiple Rails engine rspec controller test not work

I have multiple Rails engines in my Rails 4 beta1 application. 我的Rails 4 beta1应用程序中有多个Rails引擎。 I'm installed rspec-rails gem to every engines. 我为每个引擎安装了rspec-rails gem。 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: 以下是routes.rb文件的示例:

Rails.application.routes.draw do

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

When I run rspec inside first engine I get following errors: 当我在第一个引擎内运行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>'

And here is my controller test /It's generated from Rails/: 这是我的控制器测试/它是从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: 更新1:我的申请结构:

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

The solution is very simple. 解决方案非常简单。 Add use_route to your controller test. use_route添加到控制器测试中。 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 . 您显示的配置和规范适用于StoreFrontend但错误适用于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"} 当然,简单的解决方案是创建缺少的路由{: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测试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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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