我当前正在尝试断言数组中的项目为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]])
})
})
我在赛普拉斯跑步者中得到的响应如下:
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中测试此终结点时,我只得到数组[],但是当我在运行程序中运行它时,断言失败,它不会以相同的方式返回数组。如何解析响应以检查值?这些是用户权限,我需要确保用户对正确的对象具有正确的权限。
答案 0 :(得分:0)
答案 1 :(得分:0)
一种解决方法是您可以尝试断言 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')
})
})