在 RSpec 中为 PUT 接收错误,但不是 POST [英] Receiving error in RSpec for PUT, but not POST
问题描述
在我的规范中,当我运行下面的 POST 请求时,一切正常.
之前做request_payload = {玩家:{first_name: "乔",last_name: "卡拉丁",team_id: "1"}}发布:创建,request_payload结尾
但是当我为 PUT 运行规范时:
之前做request_payload = {玩家:{first_name: "巴克",last_name: "卡拉丁",team_id: "1"}}put :update, id: 3, request_payload结尾
我收到这样的错误:
[filename]_spec.rb:139: 语法错误,意外的 '\n',需要 tASSOC (SyntaxError)[filename]_spec.rb:198: 语法错误,意外的 $end,期望关键字_end
有什么想法吗?我不知道 PUT 是否有不同的语法?
要修复语法错误,请使用 put :update, { id: 3 }, request_payload
,而不是 put :update, id: 3, request_payload
.Ruby 只支持裸"(例如,无花括号)哈希作为方法的最后一个参数,因此 id: 3
不能出现在参数列表的中间,而不用花括号包裹起来.>
In my specs, when I run the POST request below, everything works fine.
before do
request_payload = {
player: {
first_name: "Joe",
last_name: "Carradine",
team_id: "1"
}
}
post :create, request_payload
end
But when I run a spec for PUT:
before do
request_payload = {
player: {
first_name: "Buck",
last_name: "Carradine",
team_id: "1"
}
}
put :update, id: 3, request_payload
end
I get an error like this:
[filename]_spec.rb:139: syntax error, unexpected '\n', expecting tASSOC (SyntaxError)
[filename]_spec.rb:198: syntax error, unexpected $end, expecting keyword_end
Any ideas? Is there a different syntax for PUT I'm not aware of?
To fix the syntax error, use put :update, { id: 3 }, request_payload
, not put :update, id: 3, request_payload
. Ruby only supports "bare" (e.g. curly-braceless) hashes as the last argument to the method, so id: 3
cannot appear in the middle of an argument list without being wrapped in curly braces.
这篇关于在 RSpec 中为 PUT 接收错误,但不是 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!