2011-07-14 35 views
3

所以我有一個看起來像這樣的路線:RoutingError與作用域路線RSpec的控制器檢測

scope "4" do 
    scope "public" do 
    scope ":apikey" do 
     resources :shops 
    end 
    end 
end 

和一堆控制器的硬件規格,其中的一個例子是這樣的:

describe ShopsController do 

    describe "when responding to a GET" do 

    context "#new" do 
     it "should create a new instance of the shop class" do 
     get :new 
     @shop.should_not_be_nil 
     end 
    end 

    end 

end 

在耙路,以及通過網絡瀏覽器,這個控制器/動作工作正常。但是,RSpec拋出:

1) ShopsController when responding to a GET#new should create a new instance of the shop class 
Failure/Error: get :new 
ActionController::RoutingError: 
    No route matches {:controller=>"shops", :action=>"new"} 
# ./spec/controllers/shops_controller_spec.rb:9:in `block (4 levels) in <top (required)>' 

當我從路由中刪除scope語句,測試工作正常。有沒有辦法「通知」RSpec的路線範圍?

在此先感謝!

回答

6

根據你的路由,:apikey是必需的參數,所以你需要將它添加到您的得到:

get :new, :apikey => "something" 

而且你應該在你的下一行改變預期到這一點:

assigns[:shop].should_not be_nil 

使用assigns檢查控制器的實例變量,並使用空格而不是下劃線將should_not與匹配器分開。最後一點需要一些習慣。

+0

像冠軍一樣工作。謝謝! – ianm