我正在尝试测试 http GET 错误响应消息,但似乎找不到任何信息或示例
预期的错误响应是:
{
"success": false,
"code": 400,
"message": "ERROR: This is the specific error message"
}
这会捕获“错误请求”,但如何验证错误响应正文中的“消息”?
expect {get "<url that generates a bad request>"}.to raise_error(/400 Bad Request/)
提前感谢您的任何见解!
最佳答案
此外:
it 'returns 400 status' do
get '/my_bad_url'
expect(response.status).to eq 400
end
你可以写
expect(JSON.parse(response.body)["message"]).to eq("ERROR: This is the specific error message")
如果您呈现 html,则不使用 JSON.parse。
关于ruby - 获取/发布错误响应的 rspec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26592014/