用capybara webkit填充params哈希

Sta*_*ers 2 ruby-on-rails capybara capybara-webkit

我有这个路由到user_friendships控制器的block动作:

block_user_friendship PUT    /user_friendships/:id/block(.:format)     user_friendships#block
Run Code Online (Sandbox Code Playgroud)

您需要知道的是阻止操作需要一个:id.

我可以在rspec规范中测试动作的结果视图,如下所示:

require 'spec_helper'
it "should not display the names of users who have a state of pending or requested" do
    visit "/user_friendships/1/block"
    page.should have_content("user with id of 1 blocked!")
end
Run Code Online (Sandbox Code Playgroud)

但这是一个脆弱的规范,因为URL是硬编码的.如何:id使用block_user_friendship_path变量填充params哈希的条目?

例如,这不起作用:

require 'spec_helper'
it "should not display the names of users who have a state of pending or requested" do
    visit block_user_friendship_path, id: "/23"
    page.should have_content("user with id of 23 blocked!")
end
Run Code Online (Sandbox Code Playgroud)

(结果 No route matches {:action=>"block", :controller=>"user_friendships"} missing required keys: [:id])

joh*_*bnz 10

visit block_user_friendship_path, id: "/23"不起作用的原因是语法.

visit block_user_friendship_path, id: "/23" 意思是:将哈希值传递id: "/23"visit-method.

你打算做什么(以及正确答案)是

visit block_user_friendship_path(id: "23")

这意味着:将id的23发送到path-helper-method.