繁体   English   中英

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

[英]Multiple Rails engine rspec controller test not work

我的Rails 4 beta1应用程序中有多个Rails引擎。 我为每个引擎安装了rspec-rails gem。 我按照命令创建了我的引擎:

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

这是我的控制器测试/它是从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

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

更新1:我的申请结构:

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

解决方案非常简单。 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

您显示的配置和规范适用于StoreFrontend但错误适用于StoreAdmin::DashboardController 因此,您似乎对您正在测试的引擎和/或哪个引擎发生故障感到困惑。

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

为了在使用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