赛普拉斯(Cypress):无法使用cy.route()存根请求 [英] Cypress: Cannot stub request with cy.route()

查看:100
本文介绍了赛普拉斯(Cypress):无法使用cy.route()存根请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下规格文件:

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');
  });
});

通过阅读赛普拉斯文档,我认为这是我们可以将http请求存根到Web服务器的方式.但是,由于我对api/v3/user的http请求返回504状态,因此无法正常工作.有没有一种方法可以使用cypress的任何命令来模拟/存根HTTP请求?我正在使用

By reading the cypress documentation, I thought that this is the way we can stub a http request to the web server. This is not working though as I expected since the http request to 'api/v3/user' returns 504 status. Is there a way to mock/stub a http request using any command of cypress ? I am using the

whatwg-fetch

whatwg-fetch

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

module in order to make request in my application.

推荐答案

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

I finally succeed to resolve the issue. The main reason that I could not stub a request is that I was using on my React-app the

window.fetch

window.fetch

来自"whatwg-fetch"模块的

方法.正如我在这里看到的, https://github.com/cypress-io/cypress/issues/687 的提取"方法存在问题.因此,当我将获取方式更改为axios时,所有问题都得到解决.

method from the 'whatwg-fetch ' module. As I saw here, https://github.com/cypress-io/cypress/issues/687 there is an issue with the 'fetch' method. So, when I changed the fetch to axios all problems were solved.

这篇关于赛普拉斯(Cypress):无法使用cy.route()存根请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆