首页 > 解决方案 > cy.request 的最简单用例不起作用

问题描述

我有以下代码:

    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 错误。请看下面的截图。我究竟做错了什么?


在此处输入图像描述

标签: cypress

解决方案


文档中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

推荐阅读