赛普拉斯 cy.request 预计 throw/fail
Cypress cy.request expect to throw/fail
有什么方法可以编写测试用例以预期 cy.request
在某些参数上失败(非 2** 响应)?
我尝试使用以下代码段:
it('intentionally fails', () => {
expect(
() => {
cy.request({
method: 'POST',
url: `https://valid.url/api/items`,
body: {name: "foo"},
})
}
).to.throw('bar')
})
但它失败了:
AssertionError
expected [Function] to throw an error
这是一个只有 API 的项目,所以基本上我到处都只使用 cy.request
。
如果没有 expect
功能块,它会失败:
CypressError
cy.request() failed on
The response we received from your web server was:
> 500: Internal Server Error
要检查响应的状态是否不是 200,请添加 failOnStatusCode: false
选项。
Cypress 捕获内部错误并且不会重新抛出它们,因此 expect(...).to.throw
不会看到任何东西。
cy.request({
method: 'POST',
// url: `https://valid.url/api/items`,
url: 'http://example.com/api/items',
body: {name: "foo"},
failOnStatusCode: false
})
.then(response => {
expect(response.status).to.be.gt(299) // status returned is 404
})
500: Internal Server Error
特定于您的 API,但 cy.request
正在执行它的工作,因为默认情况下它被配置为在请求失败时未通过测试。
有什么方法可以编写测试用例以预期 cy.request
在某些参数上失败(非 2** 响应)?
我尝试使用以下代码段:
it('intentionally fails', () => {
expect(
() => {
cy.request({
method: 'POST',
url: `https://valid.url/api/items`,
body: {name: "foo"},
})
}
).to.throw('bar')
})
但它失败了:
AssertionError
expected [Function] to throw an error
这是一个只有 API 的项目,所以基本上我到处都只使用 cy.request
。
如果没有 expect
功能块,它会失败:
CypressError
cy.request() failed on
The response we received from your web server was:
> 500: Internal Server Error
要检查响应的状态是否不是 200,请添加 failOnStatusCode: false
选项。
Cypress 捕获内部错误并且不会重新抛出它们,因此 expect(...).to.throw
不会看到任何东西。
cy.request({
method: 'POST',
// url: `https://valid.url/api/items`,
url: 'http://example.com/api/items',
body: {name: "foo"},
failOnStatusCode: false
})
.then(response => {
expect(response.status).to.be.gt(299) // status returned is 404
})
500: Internal Server Error
特定于您的 API,但 cy.request
正在执行它的工作,因为默认情况下它被配置为在请求失败时未通过测试。