Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec testing for get/post error responses

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!

like image 968
Xtalker Avatar asked Oct 27 '14 16:10

Xtalker


2 Answers

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.

like image 71
hahander Avatar answered Nov 11 '22 00:11

hahander


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.

like image 23
Alexander Karmes Avatar answered Nov 11 '22 01:11

Alexander Karmes