简体   繁体   English

赛普拉斯断言失败但测试通过

[英]Cypress assertion fails but test passes

Doing some integration tests on API.在 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
    })
  })
})

EDIT: 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")
      })
    })
  })

Why does it pass when using async/await?为什么在使用 async/await 时会通过?

测试

I see that you manage to fix your code.我看到你设法修复你的代码。 About the async/await - Cypress does not support it.关于 async/await - Cypress 不支持它。 See https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Commands-Are-Asynchronous .请参阅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.此外,它们不支持 async/await 功能,因此使用这些功能的测试可能很不稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM