我认为你看错地方了,链接显示http.post。你想要IntegrationTest post。
发件人:https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb
def post(path, **args)
process(:post, path, **args)
end
还有:
def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil)
request_encoder = RequestEncoder.encoder(as)
headers ||= {}
# rest
end
编辑:双响
Ruby 2.0 添加了关键字参数和双 splat。
当您有未知数量的参数时,基本上使用单个 splat (*),它作为 array 传递。
def with_args(*args)
p args
end
with_args(1,2,"a")
# [1, 2, "a"]
双 splat (**) 的作用类似于 *,但用于关键字参数:
def with_args(**args)
with_keyword(args)
end
def with_keyword(some_key: nil, other_key: nil)
p "some_key: #{some_key}, other_key: #{other_key}"
end
with_args(some_key: "some_value", other_key: "other_value")
# "some_key: some_value, other_key: other_value"
with_args(some_key: "some_value")
# "some_key: some_value, other_key: "
在 ruby 中,您可以调用不带() 的方法并传递不带{} 的哈希,所以
with_args({some_key: "some_value", other_key: "other_value"})
就像写作
with_args some_key: "some_value", other_key: "other_value")
看到这个答案:What does a double * (splat) operator do
和https://medium.freecodecamp.org/rubys-splat-and-double-splat-operators-ceb753329a78
所以...
写作时
post users_path, params: { user: { name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" } }
是调用过程
process(:post, users_path, params: { user: { name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" } }
process、params 中的含义是哈希
{ user: { name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" } }
process的其他关键字args无所谓,hash都是params,其他关键字都是nil
希望这是有道理的......