【问题标题】:RSpec with Ruby on Rails - Wrong number of arguments (1 for 0) when testing routesRSpec with Ruby on Rails - 测试路由时参数数量错误(1 比 0)
【发布时间】:2012-11-03 18:55:58
【问题描述】:

我尝试了测试路线,只是从 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

这里出了什么问题?

感谢您的帮助。

【问题讨论】:

  • 2.8.1,已更新,问题已解决。

标签: ruby-on-rails ruby ruby-on-rails-3 rspec tdd


【解决方案1】:

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.11 之前的旧版本 rspec,其中 expect{}.to 只是 lambda{}.should 的糖。从 2.11 开始,expect 可以接受值断言的参数,就像在 OP 的示例中一样。见:myronmars.to/n/dev-blog/2012/06/…
  • 我的 RSpec 版本原来是问题所在,因为它是旧版本。话虽如此,github.com/rspec/rspec-rails 上的 rspec-rails 文档与rubydoc.info/gems/rspec-rails/frames 不同,所以有点令人困惑......
  • 是我的 rspec 版本对我造成了同样的问题。感谢发帖!
【解决方案2】:

您的it 阻止列出/profile/:username,而您的示例列出/profiles/jsmith(请注意个人资料上的复数)。我猜应该是/profile/jsmith

【讨论】:

    【解决方案3】:

    get 命令的语法是get your_action, params =&gt; your_params。我会尝试将get :show, username =&gt; "user" 与B Seven 建议的花括号结合使用。您可能还需要将您的规范包装在诸如describe MyController do 之类的描述块中,或者可能将控制器的名称作为参数传入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多