cypress - 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 错误。请看下面的截图。我究竟做错了什么?
解决方案
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
推荐阅读
- oracle - BizTalk - 轮询 Oracle 数据库包
- c++ - X11 - XCB:窗口信息不是最新的?
- javascript - 如何在 Firebase 功能中转换印度地区的时间
- msys2 - 带有 GTK 的 Windows 10 中的 Msys2 返回错误 pkg-config
- c# - HTTP PostAsync 不返回任何内容
- c# - 通过回调将等待应用于任务
- json - Wiremock 独立 JSON response.ok 等于 false 从未达到
- javascript - JS函数在反应中没有在字符串html中被调用
- javascript - 在javascript函数中调用onclick按钮内的外部函数
- go - jemalloc 没有用 cgo 显示 go 代码的行号