首页 > 解决方案 > 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)

我究竟做错了什么?

标签: javascriptexpressfilterspecifications

解决方案


推荐阅读