赛普拉斯断言失败,但测试通过 [英] Cypress assertion fails but test passes
问题描述
在API上进行一些集成测试.当断言基本相同时,其中一项测试通过,另一项测试失败.对于赛普拉斯如何处理异步/承诺感到困惑.
Doing some integration tests on API. One of the tests passes and the other fails when assertions are basically the same. Confused about how cypress handles async/promises.
context("Login", () => {
// This test fails
it("Should return 401, missing credentials", () => {
cy.request({
url: "/auth/login",
method: "POST",
failOnStatusCode: false
}).should(({ status, body }) => {
expect(status).to.eq(401) // Passes
expect(body).to.have.property("data") // Passes
.to.have.property("thisDoesntExist") // Fails
})
})
// This test passes
it("Should return 200 and user object", async () => {
const token = await cy.task("generateJwt", users[0])
cy.request({
url: "/auth/me",
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
"Content-type": "application/json"
}
}).should(({ status, body }) => {
expect(status).to.eq(200) // Passes
expect(body).to.have.property("data") // Passes
.to.have.property("thisDoesntExist") // Fails
})
})
})
我通过执行此操作来解决它:
I fixed it by doing this:
it("Should return 200 and user object", () => {
cy.task("generateJwt", users[0]).then((result) => {
const token = result
cy.request({
url: "/auth/me",
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
"Content-type": "application/json"
}
}).should(({ status, body }) => {
expect(status).to.eq(200)
expect(body)
.to.have.property("data")
.to.have.property("thisDoesntExist")
})
})
})
为什么在使用异步/等待时会通过?
推荐答案
我看到您设法修复了代码.关于异步/等待-赛普拉斯不支持它.请参见 https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Commands-Are-异步.
I see that you manage to fix your code. About the async/await - Cypress does not support it. See https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Commands-Are-Asynchronous.
通常,赛普拉斯会将所有动作排入队列,然后执行它们.因此,如果您的代码具有任何同步部分,它将无法正常工作.此外,他们不支持异步/等待功能,因此使用这些功能进行的测试可能很不稳定.
In general, Cypress queue all the actions and then execute them. So if your code has any synchronous part it will not work. Also, they're not supporting the async/await feature, so tests using those might be flaky.
这篇关于赛普拉斯断言失败,但测试通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!