2012-07-20 72 views
8

我已經在the normal fashion中創建了一個Rails引擎,安裝了RSpec,併爲一個模型生成了一個腳手架,但是我無法獲得任何路由規範可以通過。所有路由示例都失敗了Rails 3.2引擎RSpec 2.10

這裏有一個例子:

describe Licensing::LicensesController do 
    it 'routes to #index' do 
    get('/licensing/licenses').should route_to('licensing/licenses#index') 
    end 
end 

我運行的實例在虛擬應用程序是這樣的:

$ cd spec/dummy 
$ rake spec 
/Users/brandan/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ../routing/licensing/licenses_routing_spec.rb 
F 

Failures: 

    1) Licensing::LicensesController routes to #index 
    Failure/Error: get('/licensing/licenses').should route_to('licensing/licenses#index') 
     No route matches "/licensing/licenses" 
    # /Users/brandan/repos/licensing/spec/routing/licensing/licenses_routing_spec.rb:5:in `block (2 levels) in <top (required)>' 

Finished in 0.04345 seconds 
1 example, 1 failure 

發動機能正常安裝在虛擬應用程序:

# spec/dummy/config/routes.rb 
Rails.application.routes.draw do 
    mount Licensing::Engine => "/licensing" 
end 

我可以進入虛擬應用程序並啓動控制檯並獲取該路線:

1.9.3p194 :001 > app.get('/licensing/licenses') 
    Licensing::License Load (0.3ms) SELECT "licensing_licenses".* FROM "licensing_licenses" 
200 
1.9.3p194 :002 > app.response.body 
"<!DOCTYPE html>..." 

虛擬應用程序和RSpec之間存在一些差異,我無法弄清楚它是什麼。我發現幾篇文章,聲稱能解決這個問題,但他們都沒有幫助,而且其中有幾個具體到Rails 3.1:

有沒有人在Rails 3.2/RSpec 2.10中解決了這個問題?

+0

我假設你對一個虛擬應用程序,能夠將您的發動機測試。你確定引擎是安裝在虛擬應用程序的路線上嗎?如果是的話,你確定你的規範是':type =>:routing'? – 2012-08-02 03:36:51

+0

@TanzeebKhalili我確定路由是正確的,因爲他們在控制檯和瀏覽器中工作。我從虛擬應用內運行規格。這些文件在'spec/routing'下,並且明確指定':type =>:routing'不會導致這些例子通過。還有什麼想法? : -/ – Brandan 2012-08-02 15:39:09

+1

以下更改如何:1)描述的不同對象:「描述」路徑「do ...」和2)不同的語法:'{:get =>'/ licensing/licenses'} .should be_routable' – 2012-08-03 07:17:51

回答

10

更新(2013- 10-31)

RSpec 2.14 now fully supports engine routes

module MyEngine 
    describe ExampleController do 
    routes { MyEngine::Engine.routes } 
    end 
end 

將它添加到所有路由規格:

# spec/spec_helper.rb 
config.include Module.new { 
    def self.included(base) 
    base.routes { Reportr::Engine.routes } 
    end 
}, type: :routing 

最好的解決方案我已經能夠找到迄今被明確設置,將在測試期間使用的路由集:

RSpec.configure do |config| 
    config.before(:each, type: :controller) { @routes = Licensing::Engine.routes } 
    config.before(:each, type: :routing) { @routes = Licensing::Engine.routes } 
end 

然後我不得不重寫我的路由的例子省略命名空間:

it 'routes to #index' do 
    get('/licenses').should route_to('licensing/licenses#index') 
end 

這似乎有點破解,但它確實有道理。我沒有測試Rails是否能夠在特定路徑上安裝引擎。我只測試我的引擎已經正確設置了自己的路線。不過,我寧願不必重寫一個實例變量來實現它。

下面是有關這個主題的另一對夫婦良好的線程:

+0

第二個鏈接爲我解決了這個問題。非常感謝! – 2012-12-06 13:00:17

+0

此解決方案在rspec 2.14中停止工作! – 2013-07-12 15:22:59

+0

是的,有 路線{許可:: Engine.routes} 來取代它:你可以將它添加到一個規範的,但我只需要找到一個解決方案,以增加這__all__發動機控制器規格... – mugwump 2013-11-15 10:07:28

7

我想你需要明確地通知RSpec你正在使用命名空間Rails路由。 EG,

# :use_route => ENGINE_NAMESPACE 
:use_route => 'licensing' 

此外,爲什麼不簡化的RSpec的控制器測試這樣的書面方式,

describe Licensing::LicensesController do 
    it 'GET index' do 
    get :index, use_route: 'licensing' 
    end 
end 

下面是RSpec的控制器我做了一個鏈接, https://github.com/westonplatter/questionnaire_engine/tree/engine/spec/controllers

+0

感謝您的回答,但我的問題是特別是關於路由測試 - 也就是說,我想明確測試'/ licensing/licenses'路由到正確的控制器和操作,而不僅僅是我可以在給定的控制器上得到'index'。我想我可能找到了一個解決方案,儘管這不太好。 – Brandan 2012-08-11 14:56:15