Search code examples
restcypressweb-api-testing

Getting 'expected { Object (message, detail) } to have property 'count' error in API testing using cypress


I want to assert the total count received from the response.

This is my code:

cy.request({
        method:'GET',
        url:'https://ibis-qa.droicelabs.us/api/practice/orders/?q=&limit=100',
        failOnStatusCode: false,
        headers:{
            accept: "application/json"
        }
    }).then(Response => {
        let body = JSON.parse(JSON.stringify(Response.body))
        cy.log(body)
        expect(body).has.property('count','27')
   })

and this is the error that I have got

enter image description here

Response body


Solution

  • Please use

    expect(body).has.property('count', 27)
    

    as the value is a number

    (see screen-shot, there are no quotes around 27)


    You are not getting the JSON response you think you should have.

    If go to the URL in the browser, I get this

    {"message":"field required","detail":[{"loc":["header","authorization"],"msg":"field required","type":"value_error.missing"}]}
    

    which is what is partially shown in the screenshot of the error message.

    This is an error response from the server, and it means your request is not correct.