I'm trying to test a http GET error response message and can't seem to find any info or examples of this
The expected error response is:
{
"success": false,
"code": 400,
"message": "ERROR: This is the specific error message"
}
This catches the "Bad Request" but how to verify the "message" in the body of the error response?
expect {get "<url that generates a bad request>"}.to raise_error(/400 Bad Request/)
Thanks in advance for any insight!
In adding to this:
it 'returns 400 status' do
get '/my_bad_url'
expect(response.status).to eq 400
end
you can write
expect(JSON.parse(response.body)["message"]).to eq("ERROR: This is the specific error message")
or without JSON.parse if you render html.
Request returns response, not exception:
it 'returns 400 status' do
get '/my_bad_url'
expect(response.status).to eq 400
end
Also you can read docs, to understand controller specs better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With