2012-11-03 91 views
3

我嘗試過測試路由,並從rspec-rails文檔複製了示例。RSpec與Ruby on Rails - 測試路由時參數數量錯誤(1代表0)

Failures: 

    1) routing to profiles routes /profile/:username to profile#show for username 
    Failure/Error: expect(:get => "/profiles/jsmith").to route_to(
    ArgumentError: 
     wrong number of arguments (1 for 0) 
    # ./spec/routing/test_spec.rb:11:in `block (2 levels) in <top (required)>' 

Finished in 0.001 seconds 
1 example, 1 failure 

發生了什麼事錯在這裏:

describe "routing to profiles" do 
    it "routes /profile/:username to profile#show for username" do 
    expect(:get => "/profiles/jsmith").to route_to(
     :controller => "profiles", 
     :action => "show", 
     :username => "jsmith" 
    ) 
    end 
end 

我運行RSpec的時候有以下錯誤?

感謝您的任何幫助。

+2

您的rspec版本是什麼? – pje

+1

它是2.8.1,更新和問題被解決。 – vision810

回答

2

expect需要一個塊。這意味着使用大括號:

expect{ :get => "/profiles/jsmith" }.to route_to(

參考:RSpec Expectations 2.0

我不認爲你需要想到反正。代碼Testing an RSpec controller action that can't be accessed directly

describe "routing" do 
    it "routes /auth/:provider/callback" do 
    { :post => "/auth/twitter/callback" }.should route_to(
     :controller => "authentications", 
     :action => "create", 
     :provider => "twitter") 
    end 
end 
+2

這僅適用於2.11之前的舊版rspec,其中'expect {}。to'只是'lambda {}。should'的糖。從2.11開始,期望值可以爲價值斷言提供參數,就像在OP的例子中一樣。請參閱:http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax#unification_of_block_vs_value_syntaxes – pje

+2

我的版本的RSpec原來是問題,因爲它是一個較舊的版本。話雖如此,http://github.com/rspec/rspec-rails上的rspec-rails文檔與http://rubydoc.info/gems/rspec-rails/frames不同,所以它有點令人困惑...... – vision810

+0

這是我的rspec版本對我造成同樣的問題。感謝發佈! – BenU

0

it黑名單/profile/:username,而你的例子列出/profiles/jsmith(注意曲線(S)的複數)。我猜它應該是/profile/jsmith

0

get命令的語法是get your_action, params => your_params。我會嘗試使用get :show, username => "user"與B Seven建議的花括號一起使用。您也可能需要將您的規範包裝在描述性的塊中,例如describe MyController do,或者可能傳入控制器的名稱作爲參數

相關問題