在现有项目中尝试赛普拉斯时,我遇到了路由响应的问题。此文档文章中解释了该概念:https ://docs.cypress.io/api/commands/route.html#Without-Stubbing 。
这是一个最小的非工作示例。我正在尝试获取一个空对象作为响应主体:
describe('The new event page', () => {
it('responds with the stub', () => {
cy.server();
cy.route('/dummypath', {});
cy.request('GET', '/dummypath');
});
});
存根路由清楚地显示在 GUI 中:
但响应是 404:
...具有以下主体:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /dummypath</pre>
</body>
我认为 404 响应是由我的实际服务器而不是 cy.server() 发送的。实际的服务器正在运行,我在 my: -filelocalhost:3000
中指定了它。baseUrl
cypress.json
有没有人见过类似的东西?我是否忽略了代码中的任何明显错误?
PS: 当我将端口号更改为其他未使用的端口时,错误会变为网络错误(这可能是意料之中的)。
CypressError: cy.request() failed trying to load:
http://localhost:3002/dummypath
We attempted to make an http request to this URL but the request failed without a response.
We received this error at the network level:
> Error: connect ECONNREFUSED 127.0.0.1:3002
-----------------------------------------------------------
The request we sent was:
Method: GET
URL: http://localhost:3002/dummypath
-----------------------------------------------------------
Common situations why this would fail:
- you don't have internet access
- you forgot to run / boot your web server
- your web server isn't accessible
- you have weird network configuration settings on your computer