【发布时间】:2020-12-05 10:24:17
【问题描述】:
我在 Cypress 6 中遇到数据存根问题。我正在尝试实现从服务器到自定义数据的真实数据交换。我阅读了文档并得出结论
describe("test", () => {
it("intercept", () => {
cy.intercept("http://localhost:3000/spoons", (req) => {
req.reply((res) => {
let { body } = res;
body.newProperty = "new";
console.log(res.body);
return body;
});
});
});
});
将是解决方案,但是...网络中的请求 http://localhost:3000/spoons 的正文返回给我
{
"sizes": [
"huge",
"small"
],
"colors": [
"yello",
"brown"
],
"build": {
"back": true,
"front": true
}
}
但是在 console.log 中显示 res.body 有什么,它得到一个 空的 console.log,就好像它没有任何 res.body 一样。
编辑#1
关于内部“服务器”,我用一个网站制作了一个简单的快速服务器,该网站使获取请求可以在“网络”中轻松获得另一个请求。它只是作为训练intercept 和其他东西的战场。这个/spoons有唯一的端点
server.js
const express = require("express");
const app = express();
const port = 3000;
const path = require("path");
const obj = {
sizes: ["huge", "small"],
colors: ["yello", "brown"],
build: {
back: true,
front: true,
},
};
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/index.html"));
});
app.get("/spoons", (req, res) => {
res.json(obj);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
<script>
console.log(fetch("http://localhost:3000/spoons"));
</script>
</html>
【问题讨论】:
标签: javascript automated-tests cypress