繁体   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