我尝试测试路由,只是从 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
占用一个 block 。这意味着使用大括号:
expect{ :get => "/profiles/jsmith" }.to route_to(
我认为无论如何你都不需要期待。来自 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
关于ruby-on-rails - RSpec with Ruby on Rails - 测试路由时参数数量错误(1 代表 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212410/