cy.request的简单用例不起作用

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

我有以下代码:

    cy.server();
    cy.route({
        method: 'GET',
        url: 'http://localhost:4200/api/login',
        response:{sadasda:2312312123}})
    cy.visit('http://localhost:4200/');
    cy.request('GET', '/api/login');

但是cy.request会抛出404错误。请参阅下面的截图。我究竟做错了什么?


enter image description here
cypress
1个回答
1
投票

来自docs for cy.request

cy.request()不能用于调试cy.server()和cy.route()

cy.request()将请求发送到实际端点,绕过使用cy.route()定义的端点

cy.request()的目的是用于检查实际运行的服务器上的端点,而无需启动前端应用程序。

cy.route()旨在用于测试您的应用程序,而不是测试cy.request()。尝试从您的应用程序中创建一个与cy.route()匹配的XmlHttpRequest,它将起作用:

const xhr = new XmlHttpRequest()
xhr.open('GET', 'http://localhost:4200/api/login', false)
xhr.send() // now your `cy.route` will trigger
© www.soinside.com 2019 - 2024. All rights reserved.