簡體   English   中英

帶有Ruby on Rails的RSpec - 測試路由時參數數量為1(0表示為0)

[英]RSpec with Ruby on Rails - Wrong number of arguments (1 for 0) when testing routes

我嘗試過測試路由,只是復制了rspec-rails文檔中的示例。

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時出現以下錯誤:

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

這里出了什么問題?

謝謝你的幫助。

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

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

參考: RSpec Expectations 2.0

無論如何,我認為你不需要期待。 代碼來自測試無法直接訪問的RSpec控制器操作

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

it塊列表/profile/:username ,而你的榜樣名單/profiles/jsmith (注意曲線(S)的復數)。 我猜它應該是/profile/jsmith

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM