【问题标题】:Making assertions on number of HTTP calls对 HTTP 调用次数进行断言
【发布时间】:2019-04-10 03:23:54
【问题描述】:

我打算在 cypress 中设置一些基本测试,使用 cy.server() cy.route() 和 cy.wait() 对一些基本的 http 调用进行存根。在我的应用程序中,我需要断言路由被存根的次数。例如,当我提交表单时,它会向 localhost:3001 发送请求,如果我再次提交表单,它将再次提交。我想断言提交的数量。像 expect(xhr).to.have.lenght(2) 或类似的东西,但我不知道怎么做。

这是我目前的简单测试

it("Checking number of requests", () => {
    cy.server();
    cy.route({
      method: "POST",
      url: "**/signupNewUser*",
      response: {
        kind: "identitytoolkit#SignupNewUserResponse",
        idToken: "sarasa",
        email: "sarasa@gmail.com",
        refreshToken: "sarasa2",
        expiresIn: "3600",
        localId: "sarasa3",
        customTestingProperty: "custom"
      }
    }).as("postAPI");

    cy.fillForm(password);
    cy.contains("Submit").click();
    cy.contains("Submit").click();

    // Here I would like to assert that "**/signupNewUser*" has been requested 2 times. 
// I don't want to test for how many times has the button been clicked or the form been submitted.

  });

【问题讨论】:

    标签: testing frontend cypress


    【解决方案1】:

    基于 Cypress 源上的 GitHub 线程 - https://github.com/cypress-io/cypress/issues/477#issuecomment-412617742

    实际上有一种未记录的方法可以检查在别名上使用 .all 响应 XHR 的次数。

    cy.wait('@postAPI')
    cy.tick(10000)
    cy.get('@postAPI.all').should('have.length', 2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2023-03-23
      • 2019-05-19
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      相关资源
      最近更新 更多