[英]Cypress assertion fails but test passes
在 API 上做一些集成測試。 當斷言基本相同時,一個測試通過,另一個失敗。 對賽普拉斯如何處理異步/承諾感到困惑。
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
})
})
})
編輯:我通過這樣做來修復它:
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")
})
})
})
為什么在使用 async/await 時會通過?
我看到你設法修復你的代碼。 關於 async/await - Cypress 不支持它。 請參閱https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Commands-Are-Asynchronous 。
通常,賽普拉斯將所有操作排隊,然后執行它們。 因此,如果您的代碼有任何同步部分,它將無法工作。 此外,它們不支持 async/await 功能,因此使用這些功能的測試可能很不穩定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.