我有以下代码:
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