我是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允许在定义参数时指定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
Http状态代码始终为整数。这是行业标准。您无法更改http状态代码或将http状态代码作为字符串发送。
在此处查找http状态代码列表。 https://www.restapitutorial.com/httpstatuscodes.html
这不是特定于铁轨或葡萄或任何其他框架。