2

我想測試一個Rails 3.1引擎與RSpec 2.經過大量的試驗和錯誤(和文檔和堆棧溢出搜索)該應用程序正在工作,我已經獲得最多的規格通過。問題是我的路線規格仍然失敗。測試Rails 3.1與RSpec 2引擎路由

對於發動機 「富」 與分離的命名空間和控制器的Foo :: BarsController,我有這樣的:

require "spec_helper" 

describe Foo::BarsController do 
    describe "routing" do 
    it "routes to #index" do 
     get("/foo/bars").should route_to("bars#index") 
    end 

    it "routes to #new" do 
     get("/foo/bars/new").should route_to("bars#new") 
    end 
    end 
end 

這導致:

1) Foo::BarsController routing routes to #index 
    Failure/Error: get("/foo/bars").should route_to("bars#index") 
    ActionController::RoutingError: 
    No route matches "/foo/bars" 
    # ./spec/routing/foo/bars_routing_spec.rb:6:in `block (3 levels) in <top (required)>' 

2) Foo::BarsController routing routes to #new 
    Failure/Error: get("/foo/bars/new").should route_to("bars#new") 
    ActionController::RoutingError: 
    No route matches "/foo/bars/new" 
    # ./spec/routing/foo/bars_routing_spec.rb:10:in `block (3 levels) in <top (required)>' 

我的規格虛擬應用似乎設置正確:

Rails.application.routes.draw do 
    mount Foo::Engine => "/foo" 
end 

如果有助於回答這個問題,我的查看規格也是不工作。這裏是一個典型的錯誤:

9) foo/bars/index.html.erb renders a list of bars 
    Failure/Error: render 
    ActionView::Template::Error: 
    undefined local variable or method `new_bar_path' for #<#<Class:0x00000100c14958>:0x0000010464ceb8> 
    # ./app/views/foo/bars/index.html.erb:3:in `___sers_matt__ites_foo_app_views_foo_bars_index_html_erb___1743631507081160226_2184232780' 
    # ./spec/views/foo/bars/index.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>' 

任何想法?

回答

1

試着改變你的get方法使用特定的路線

get("/foo/bars", :use_route => :foo) 
+0

似乎並沒有得以順利。 'ArgumentError:錯誤的參數個數(2代表1)' –

+1

我認爲混淆來自這樣一個事實:在這個上下文中'get'是RSpec的'get'方法,而上面是Rails方法。 –

0

實際上並沒有很難讓這個工作。這是有點冒失的,因爲你正在向引擎的routes.rb文件添加代碼,該文件根據運行的環境而變化。如果你使用spec/dummy site approach來測試引擎,那麼在/ config/routes中使用下面的代碼片段.rb文件:

# <your_engine>/config/routes.rb 

if Rails.env.test? && Rails.application.class.name.to_s == 'Dummy::Application' 
application = Rails.application 
else 
application = YourEngine::Engine 
end 

application.routes.draw do 
... 
end 

這基本上是做什麼的是將虛擬應用程序的引擎切換出來,並在測試模式下寫入路由。