【问题标题】:Cypress: Cannot stub request with cy.route()赛普拉斯:无法使用 cy.route() 存根请求
【发布时间】:2018-02-26 11:26:57
【问题描述】:

我有以下规格文件:

describe('The First Page', () => {
  beforeEach(() => {
    cy.server();
    cy.route({
      method : 'GET',
      url: '**/v3/user/*',
      status: 204,
      response: {
        id: 1,
        firstName: 'Dev',
        lastName: 'Dev',
        email: 'dev1@gmail.com',  
      },
    });
  });
  it('successfully loads', () => {
    cy.visit('/dashboard/account');
  });
});

通过阅读the cypress documentation,我认为这是我们可以将 http 请求存根到 Web 服务器的方式。尽管对 'api/v3/user' 的 http 请求返回 504 状态,但这并不像我预期的那样工作。有没有办法使用 cypress 的任何命令来模拟/存根 http 请求?我正在使用

whatwg-fetch

模块以便在我的应用程序中发出请求。

【问题讨论】:

    标签: tdd integration-testing e2e-testing cypress


    【解决方案1】:

    我终于成功解决了这个问题。我无法存根请求的主要原因是我在我的 React-app 上使用了

    window.fetch

    来自“whatwg-fetch”模块的方法。正如我在这里看到的,https://github.com/cypress-io/cypress/issues/687 'fetch' 方法存在问题。因此,当我将 fetch 更改为 axios 时,所有问题都解决了。

    【讨论】:

    • 我一直在使用axios。你能想到其他可能是问题的地方吗?
    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2021-10-26
    • 2020-12-23
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多