我是Rails编程的新手,并且在提出葡萄异常时尝试使用rails status code
,但是我的测试失败了。我在葡萄文档中找不到任何示例,因此这可能是不可能的。
我想知道是否有可能,为什么可以?
这有效:
rescue_from Example::IsExampleWorking do |_e|
error!({ messages: ["Example is not working because it was not found"] }, 404)
end
但这不是:
rescue_from Example::IsExampleWorking do |_e|
error!({ messages: ["Example is not working because it was not found"] }, :not_found)
end
我的测试是:
it "return a 404 Not Found status" do
expect(response).to have_http_status(:not_found)
end
编辑:伙计们,我忘了提到他测试的错误消息是
undefined method `to_i' for :not_acceptable:Symbol
Did you mean? to_s
但是我没有在Grape的文档中找到任何文档来确保它们仅接受整数作为第二个参数。
谢谢:)
答案 0 :(得分:0)
在定义参数时,Grape允许指定type
。
params do
# Basic usage: require a parameter of a certain type
requires :param_name, type: Integer
end
请参阅:https://www.rubydoc.info/github/ruby-grape/grape/Grape%2FDSL%2FParameters:requires
答案 1 :(得分:0)
Http状态代码始终仅是整数。那是行业标准。您不能更改http状态代码或将http状态代码作为字符串发送。
在此处查找http状态代码列表。 https://www.restapitutorial.com/httpstatuscodes.html
这并非特定于rails或grape或任何其他框架。