2013-01-18 28 views
2

我在測試新手,使用RSpec的,需要一些幫助:命名路線在rspec的例如組參數

我有共同的例子組:

shared_examples_for 'request which do something' do |opts = {}| 
    respond.should redirect_to(opts[:redirect_to]) 
end 

在我的規格文件:

describe "behavior" do 
    it_should_behave_like 'request which do something', :redirect_to => root_path 
end 

看起來不錯,但我得到這個錯誤:

Exception encountered: #<NameError: undefined local variable or method `root_path' for #<Class:0x000000069e2838>> 

,並將其與分線「it_should_behave_like ......」

我想包括Rails.application.routes.url_helper在spec_helper,但它不反正工作。

通過它完美的例子是這樣的方式:

describe "behavior" do 
    it "should redirect" do 
    response.should redirect_to(root_path) 
    end 
end 

(即使沒有明確包括url_helpers)

感謝您的幫助。

回答

2

您不能在示例組中使用路徑幫助程序,但有一種解決方法。看到這個答案:

Passing a named route to a controller macro in RSpec

有了這個,你可以通過一個符號,並使用send

+0

謝謝。是的,我也考慮過這個問題,但擔心我錯過了一些東西,還有另一種方式。 我很感謝你的快速回答!再次感謝。 – evilguc