2
votes

I am currently trying to assert the items in an array to be true. This is how the test is currently written.

    it.only('GET getUserPermissions', () => {

        cy.request({
            method: 'get',
            failOnStatusCode: false,
            log: true,
            url: 'https://someurl.org/api/getUserPermissions?=1234',
            headers: {
                //'accept': 'application/json',
                'authorization': 'Bearer ' + login.jwt
            },
            response: []
        }).then((response) => {
            console.log(response.body)
            assert.equal(response.status, 200)
            expect(response.body).to.not.be.null
            console.log(response)
            expect(response.body).to.contain(
                [[[43239,"admin",136,4],[43257,"database.events",165,4],43258,"deadbeat.list",113,4],43266,"navigation",6,4],[43267,"object",19,4],[43272,"qa.object",31,4],43278,"user",5,4]])
        })
    })

The response I get in the Cypress runner looks like this:

TEST
1 REQUEST GET 200 https://https://someurl.org/api/getUserPermissions?=1234
2 -ASSERT expected 200 to equal 200  --Pass
3 -ASSERT expected [ Array(7) ] not to be null --Pass
4 -ASSERT expected [ Array(7) ] to include [[[43239,"admin",136,4],[43257,"database.events",165,4],43258,"deadbeat.list",113,4],43266,"navigation",6,4],[43267,"object",19,4],[43272,"qa.object",31,4],43278,"user",5,4]]  --Fails

When I test this endpoint in Postman, I get just the array [], but when I run this in the runner, the assertion fails, it's not returning the array in the same manner. How do I parse the response to check the values? These are user permissions and I need to ensure that the user has the correct permissions for the correct object.

2
Try expect(response.body).to.deep.equal([[[43239,"admin",136,4],.... - user14783414

2 Answers

1
votes

One workaround is you can try to assert the type like

  expect(arrayname).to.be.a('array')

where 'array' is nothing but the type. In our case, we will be using response.body in the place of arrayname.

Similarly, we can use array as the type

Try this:

 it.only('GET getUserPermissions', () => {

    cy.request({
        method: 'get',
        failOnStatusCode: false,
        log: true,
        url: 'https://someurl.org/api/getUserPermissions?=1234',
        headers: {
            //'accept': 'application/json',
            'authorization': 'Bearer ' + login.jwt
        },
        response: []
    }).then((response) => {
        console.log(response.body)
        assert.equal(response.status, 200)
        expect(response.body).to.not.be.null
        console.log(response)
        expect(response.body).to.be.a('array')
    })
})
0
votes

If you want to compare the contents of the array, you could just loop through the array and compare each value on its own.

If you have an the exact copy of the original array to compare against, the you could deeply compare the arrays. You can find some examples of using deep assertions with Cypress here, or with Chai here.