testing - 对 HTTP 调用次数进行断言

标签 testing frontend cypress

我打算在 cypress 中设置一些基本测试,使用 cy.server() cy.route() 和 cy.wait() 对一些基本的 http 调用进行 stub 。在我的应用程序中,我需要断言路由被 stub 的次数。例如,当我提交表单时,它会向 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.

  });

最佳答案

基于 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)

关于testing - 对 HTTP 调用次数进行断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604306/

相关文章:

java - 如何将 Spring @Autowired 所需的属性设置为 false 以进行测试?

javascript - 仅显示首次访问 Django 的介绍视频

javascript - 动态数据表Jquery

javascript - 在javascript中将图像添加到数组

node.js - Cypress CLI 控制台输出不太可读

node.js - 避免使用 cy.wait() 等待页面由于中止的获取请求而加载 cypress

unit-testing - 为单元测试启动嵌入式 Cassandra 实例

iphone - 将签名的应用程序部署到越狱 iPhone

testing - jmeter 在 Listener 中的工作顺序是什么?

Cypress 测试未启动并在控制台中抛出握手错误