3

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

这里出了什么问题?

谢谢你的帮助。

4

3 回答 3

2

expect需要一个块。这意味着使用大括号:

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

参考:RSpec 期望 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
于 2012-11-03T20:20:57.193 回答
0

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

于 2012-11-03T20:25:02.943 回答
0

get 命令的语法是get your_action, params => your_params. 我会尝试get :show, username => "user"按照 B Seven 的建议与花括号一起使用。您可能还需要将您的规范包装在一个描述块中,例如describe MyController do,或者可能将控制器的名称作为参数传递

于 2012-11-04T01:44:58.820 回答