Rspec check for get / post error responses

I am trying to check the HTTP GET error message and cannot find any information or examples of this

Expected error response:

{
  "success": false,
  "code": 400,
  "message": "ERROR: This is the specific error message"
}

      

This raises a "bad request", but how do you check 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 your understanding!

+3


source to share


2 answers


The request returns a response, not an exception:

it 'returns 400 status' do
  get '/my_bad_url'
  expect(response.status).to eq 400
end

      



Also you can read the docs to better understand the controller specifications.

+4


source


In addition 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 are generating html.

+2


source







All Articles