Cypress API 测试,预计有两种可能的响应

问题描述 投票:0回答:1

在“before”钩子中,我有一堆 API 请求设置我的测试用户进行测试,其中一个方法负责设置密码,并且可能必须出现:

  1. 204 如果用户还没有任何密码,那么它会创建一个新密码
  2. 400(如果用户已有密码)

我希望测试在这两种情况下都不会失败,是否可以在响应中添加 2 个可能的结果?

我尝试过类似的方法,但它不起作用,eq() 中的第二个值被完全忽略

cy.request({
method: 'PUT',
url: "url/endpoint"
headers: {
  Authorization: "Bearer " + Cypress.env("access_token_val"),
  Connection: "keep-alive",
  Host: "host",
},
body: {
  "type": "password",
  "value": password,
  "temporary": false
}
}).then((response) => {
  expect(response.status).to.eq(204||400) })
javascript typescript api cypress
1个回答
0
投票

您的代码不起作用,因为

204||400
返回
204

console.log(204||400);

expect(response.status).to.eq(204||400)
expect(response.status).to.eq(204)
相同。

您可以手动查看预期结果

expect(response.status === 204 || response.status === 400).to.be.true
© www.soinside.com 2019 - 2024. All rights reserved.