簡體   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