1

我正在做一些测试,我拦截了一些对同一个 url 的 api 调用,我在 beforeEach 之前做了一个,然后在测试中做了另一个,但由于某种原因,我不明白我更改了别名。我正在阅读,并且覆盖已修复,但显然不是?请随时提出更多问题。希望我能得到一些意见。

我的代码:

// Describe block:

  beforeEach(() => {
    cy.visit("/");
    cy.intercept(
      {
        method: "GET",
        url: "/customers*",
        hostname: "local.api",
      },
      {
        fixture: "customers.json",
      }
    ).as("customers");
    cy.get("[class^=ant-menu-item]", { multiple: true }).eq(1).click();
    cy.wait("@customers");
  });



  [
    ["customerName", "ASC", 0],
    ["nextReviewDate", "ASC", 1],
    ["nextReviewType", "ASC", 2],
  ].forEach(([sortValue, sortOrder, index]) => {
    it(`Sort by direction ${sortOrder} order ${sortValue}`, () => {
      cy.get(".ant-table-column-sorters", { multiple: true }).eq(index).click();
      cy.intercept("GET", "/customers*").as("request");
      cy.wait("@request").then((interception) => {
        cy.wrap(interception.response.statusCode).should("eq", 200);

        cy.wrap(interception.request.url).should(
          "include",
          `https://allica.local.api/customers?page=1&sortBy=${sortValue}&pageSize=5&direction=${sortOrder}`
        );
      });
    });
  });

以下错误:

在此处输入图像描述

如果没有覆盖,如何克服这个测试?

提前致谢。

4

1 回答 1

0

拦截是一个事件监听器。必须在事件触发前设置

cy.intercept("GET", "/customers*").as("request");
cy.get(".ant-table-column-sorters", { multiple: true }).eq(index).click();
cy.wait("@request").then((interception) => {
  ...

实际上,测试之间的截距没有变化,因此您只需设置一次并等待多次

before(() => cy.intercept("GET", "/customers*").as("request"))

[
  ["customerName", "ASC", 0],
  ["nextReviewDate", "ASC", 1],
  ["nextReviewType", "ASC", 2],
].forEach(([sortValue, sortOrder, index]) => {
  it(`Sort by direction ${sortOrder} order ${sortValue}`, () => {
    cy.get(".ant-table-column-sorters").eq(index).click();
    cy.wait("@request").then((interception) => {
于 2022-02-18T11:00:20.390 回答