使用Ruby on Rails的RSpec—测试路由时错误的参数数量(1为0)

时间:2022-08-03 23:20:28

I tried testing routes and just copied the example from the rspec-rails documentation.

我尝试了测试路由,并从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

I got the following error when running RSpec:

我在运行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

What's going wrong here?

什么错了吗?

Thanks for any help.

感谢任何帮助。

3 个解决方案

#1


2  

expect takes a block. This means use curly brackets:

预计需要一个街区。这意味着使用花括号:

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

Reference: RSpec Expectations 2.0

参考:RSpec预期2.0

I don't think you need expect anyway. Code from Testing an RSpec controller action that can't be accessed directly

我认为你不需要期待。来自测试无法直接访问的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

#2


0  

Your it block lists /profile/:username while your example lists /profiles/jsmith (note the plural on profile(s)). I'm guessing it should be /profile/jsmith.

您的it块列出/配置文件/:用户名,而您的示例列出/配置文件/jsmith(注意配置文件中的复数)。我猜应该是/profile/jsmith。

#3


0  

The syntax for the get command is get your_action, params => your_params. I would try using get :show, username => "user" in conjunction with curly braces as B Seven suggested. You also may need to wrap your spec in a describe block such as describe MyController do, or possibly pass in the name of the controller as parameter

get命令的语法是get your_action, params => your_params。我将尝试使用get:show, username => "user"结合花括号,如b7所建议的。您还可能需要将您的规范封装在一个描述块中,比如describe MyController do,或者可能将控制器的名称作为参数传递

#1


2  

expect takes a block. This means use curly brackets:

预计需要一个街区。这意味着使用花括号:

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

Reference: RSpec Expectations 2.0

参考:RSpec预期2.0

I don't think you need expect anyway. Code from Testing an RSpec controller action that can't be accessed directly

我认为你不需要期待。来自测试无法直接访问的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

#2


0  

Your it block lists /profile/:username while your example lists /profiles/jsmith (note the plural on profile(s)). I'm guessing it should be /profile/jsmith.

您的it块列出/配置文件/:用户名,而您的示例列出/配置文件/jsmith(注意配置文件中的复数)。我猜应该是/profile/jsmith。

#3


0  

The syntax for the get command is get your_action, params => your_params. I would try using get :show, username => "user" in conjunction with curly braces as B Seven suggested. You also may need to wrap your spec in a describe block such as describe MyController do, or possibly pass in the name of the controller as parameter

get命令的语法是get your_action, params => your_params。我将尝试使用get:show, username => "user"结合花括号,如b7所建议的。您还可能需要将您的规范封装在一个描述块中,比如describe MyController do,或者可能将控制器的名称作为参数传递