javascript - JavaScript / Node.js / Express.js : HTTP Route Filtering for Completion Status from Data Object --> 测试规范
问题描述
我正在使用 Node.js 和 Express。我正在尝试设置路由器 HTTP 请求。
测试规范如下:
describe("query filtering (?key=value)", function() {
beforeEach(function() {
todos.add("billy", { content: "learn about req.query" });
todos.complete("billy", 0);
todos.add("billy", { content: "enable requests for specific todos" });
});
我有上述测试规范正在调用的函数。上述测试规范返回:
{ billy:
[ { content: 'learn about req.query', complete: true },
{ content: 'enable requests for specific todos',
complete: false } ] }
剩下的测试规范是:
it("GET can get just the completed tasks", function() {
return supertest
.get("/users/billy/tasks?status=complete")
.expect(200)
.expect("Content-Type", /json/)
.expect(function(res) {
expect(res.body).to.have.length(1);
expect(res.body[0].content).to.equal("learn about req.query");
});
});
下面是我用来尝试通过测试规范的代码:
// GET can get just the completed tasks
router.get("/users/:name/tasks?status=complete", (req, res, next) => {
let name = req.params.name;
// req.query would yield {status: complete}
if (req.query.status === "complete") {
const completedTasks = todos
.list(name)
.filter(todoTask => todoTask.complete === true);
res.send(completedTasks);
}
});
但它没有通过错误:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
我究竟做错了什么?
解决方案
推荐阅读
- excel - .Value = Evaluate("SUMPRODUCT") returning #VALUE but .Value = "=SUMPRODUCT" works correctly?
- java - 使用 maven-bundle-plugin 嵌入/包含生成的源
- sql - 如何更新使用 Union 的表
- google-apps-script - 如何使用 curl 从 Google Mobile Friendly Test API 查看呈现的代码?
- julia - Editing a value inside a for loop
- python - Creating a list of class instances
- docusignapi - How to populate Signer Name text field in document via REST API
- html - How can I center my posts and pages in blogger?
- database - PACELC 定理是一个定理还是一个猜想
- java - ojdbc 升级 oci 连接 url 不再工作