如何在Chai中验证api响应数组是否包含无索引值



如果我发出一个api请求,得到的响应看起来像这样:

{
applicationVersion: "0.0.0.0",
healthResults: [
{
name: "Api1",
responseTimeInMilliseconds: 256,
isAvailable: true,
customProperties: {}
},
{
name: "Api2",
responseTimeInMilliseconds: 539,
isAvailable: false,
customProperties: {}
},
{
name: "Api3",
responseTimeInMilliseconds: 256,
isAvailable: true,
customProperties: {}
}
]
}

在Chai(Cypress(中,我如何在不使用索引的情况下编写一个断言来验证每个名为x、isAvailable设置为true的api,因为healthResults数组在不同的服务器上可能有不同的顺序。这行不通:

expect(response.body.healthResults[0].isAvailable).to.be.true

Cypress真实世界应用程序中有许多API测试示例,这是一个演示Cypress测试方法、模式和工作流在现实世界中的使用的支付应用程序。查看示例,了解针对API响应使用Cypress断言。

最新更新