【问题标题】:supertest nodejs test get callsupertest nodejs 测试获取调用
【发布时间】:2014-09-02 03:56:00
【问题描述】:

我正在构建一个 API,并尝试使用 mocha 和 supertest 对其进行测试。

我正在使用此代码正确测试 POST 调用:

it("Should generate a PDF based on the given data using API", function(done) {

    request(app)
    .post("/api/document/print")
    .send({tplName: "default", tplData: { title: "Testee", p1: "paragraph"}})
    .expect(200, done);

});

但是当我尝试使用此代码测试 GET 请求时:

it("Should get HTML of the selected template", function(done) {

    request(app)
    .get("/api/template/default/html")
    .expect(200, done);

});

测试失败,如果我运行我的应用并在 Chrome 中尝试,我会得到正确的响应 (200)。

我做错了什么?

【问题讨论】:

  • 我不知道。测试失败时会说什么?
  • 好吧..没什么。它只是失败了。
  • 你的测试没有给你任何输出?
  • npm ERR! Test failed. See above for more details. 但上面什么都没有。
  • 你在 package.json 中的 scripts.test 命令是什么(如果是 make test,那么你的 Makefile 中的 test 是什么)?

标签: node.js supertest


【解决方案1】:

这些测试是否在同一个文件中?在同一个描述块内?也许你可以试试这个:

.expect(200) .end(function(err, res){ if (err) return done(err); done() });

【讨论】:

  • @FezVrasta,你为什么不尝试在期望调用之外调用 done
猜你喜欢
  • 1970-01-01
  • 2020-02-15
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多