对HTTP调用的数量进行断言

问题描述 投票:0回答:1

我想在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: "[email protected]",
        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个回答
0
投票

基于赛普拉斯源上的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)
© www.soinside.com 2019 - 2024. All rights reserved.