赛普拉斯存根似乎从实际服务器产生响应

时间:2017-11-06 16:18:10

标签: javascript testing stubbing cypress

在现有项目中尝试赛普拉斯,我遇到了对路由的存根响应问题。本文档文章中解释了该概念: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中:

Picture showing the routes registered in the Cypress GUI

但回复是404:

Server responds with a 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()发送的。实际服务器正在localhost: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

1 个答案:

答案 0 :(得分:2)

cy.request()向指定的网址发出实际的HTTP请求。如果您不想加载实际应用程序,则应使用此命令。例如,您可能想检查服务器上的端点。

cy.route()用于处理您正在测试的应用程序中发出的HTTP请求。

如果要对正在测试的应用程序中的

中的 进行存根响应,则可能需要使用execution error: Network file permission error. (-5000) cy.route()的组合。例如,为了确保当我们访问我们的应用程序时,我们的应用程序向.wait()发出GET请求,并且对此请求的响应是我们的存根/dummypath,我们会写:

{}