我目前正在尝试断言数组中的项为true。这就是当前测试的编写方式。
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]])
})
})
我在Cypress runner中得到的回应是这样的:
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
当我在Postman中测试这个端点时,我只得到了array[],但当我在runner中运行它时,断言失败了,它没有以相同的方式返回数组。如何解析响应以检查值?这些是用户权限,我需要确保用户对正确的对象具有正确的权限。
一个解决方法是可以尝试断言类似于的type
expect(arrayname).to.be.a('array')
其中"array"只是类型。在我们的例子中,我们将使用response.body
来代替arrayname。
类似地,我们可以使用数组作为类型
试试这个:
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')
})
})
如果您想比较数组的内容,您可以循环遍历数组并单独比较每个值。
如果您有原始数组的精确副本可供比较,则可以深入比较这些数组。您可以在这里找到一些将deep
断言与Cypress或Chai一起使用的示例。