I am testing for a API en RoR
. I want to validate the structure of a response to a request from my API. I want to make sure that a particular field exists in that response and if it doesn't exist indicate that the test fails.
let(:user) { FactoryGirl.create(:user) }
describe "POST #create" do
context "when logged in with identifier" do
before(:each) do
@user_attributes = { identifier: user[:email], password: '12345678' }
post :create, { session: @user_attributes }
end
it"when is valid token" do
#como valido que en la respuesta exista el token
end
end
end
The response
petition is as follows:
{"user":
{
"id":9005,
"email":"[email protected]",
"first_name":"Serenity",
"middle_name":"Keagan",
"last_name":"O'Kon",
"birthday":"2014-07 -01T04:30:00.000Z"
},
"token":"....."
}
I am using RSpec 3.3.0
andRails 4.2.3
When you make a call to post, you get or should get an object as a response
response
. I recommend that you parse it to JSON to manipulate it better.In general, the key is to parse as JSON and do something with the object
response.body
.