Mocha,测试对数组的放置请求

Mocha, testing a put request to an array

我正在尝试更新我的数组:

let myDB = [{ post: "post_1", comment: "comment_1" }];

有一个放置请求

app.put("/updatePost", function(req, res) {
  let index = Number(req.body.updateI) - 1;

  let updatedComment = { post: req.body.updateP, comment: req.body.updateC };
  myDB.splice(index, 1, updatedComment);

  res.render("index.ejs", { posts: myDB });

});

手动运行时工作正常,但Mocha/SuperTest测试失败:

describe("Put /", function() {
  it("I should access the Put route ", function(done) {
    supertest(myApp.app)
      .put("/updatePost")
      .send({
        updateI: "1",
        updateP: "update_post_1",
        updateC: "update_comment_1"
      })
      .expect(res => {
        if (myApp.myDB[0].updateP !== "update_post_1") {
          throw new Error("Update error" );
        }
     })
      .expect(200)
      .end(function(err, res) {
        if (err) return done(err);
        done();
      });
  });
});

具体来说,Mocha/SuperTest 中的 myApp.myDB[0].updateP 测试 returns undefined 而不是字符串。有什么想法吗?

你应该改变:

if (myApp.myDB[0].updateP !== "update_post_1") {
  throw new Error("Update error");
}

至:

if (myApp.myDB[0].post !== "update_post_1") {
  throw new Error("Update error");
}

例如

app.js:

const express = require("express");
const bodyParser = require("body-parser");
const app = express();

const myDB = [{ post: "post_1", comment: "comment_1" }];

app.use(bodyParser.json());
app.put("/updatePost", (req, res) => {
  let index = Number(req.body.updateI) - 1;

  let updatedComment = { post: req.body.updateP, comment: req.body.updateC };
  myDB.splice(index, 1, updatedComment);

  res.json({ posts: myDB });
});

module.exports = {
  app,
  myDB,
};

app.test.js:

const myApp = require("./app");
const supertest = require("supertest");

describe("Put /", function() {
  it("I should access the Put route ", function(done) {
    supertest(myApp.app)
      .put("/updatePost")
      .send({
        updateI: "1",
        updateP: "update_post_1",
        updateC: "update_comment_1",
      })
      .expect((res) => {
        console.log(myApp.myDB);
        if (myApp.myDB[0].post !== "update_post_1") {
          throw new Error("Update error");
        }
      })
      .expect(200)
      .end(function(err, res) {
        if (err) return done(err);
        done();
      });
  });
});

集成测试结果与覆盖率报告:

  Put /
[ { post: 'update_post_1', comment: 'update_comment_1' } ]
    ✓ I should access the Put route  (44ms)


  1 passing (51ms)

-------------|----------|----------|----------|----------|-------------------|
File         |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files    |    90.91 |       50 |      100 |    95.24 |                   |
 app.js      |      100 |      100 |      100 |      100 |                   |
 app.test.js |    81.82 |       50 |      100 |       90 |                16 |
-------------|----------|----------|----------|----------|-------------------|

正如您在输出中看到的,myDB 的值为

[ { post: 'update_post_1', comment: 'update_comment_1' } ]

myApp.myDB[0]会给你

{ post: 'update_post_1', comment: 'update_comment_1' }

此对象中没有 updateP 字段。这就是为什么 myApp.myDB[0].updatePundefined.

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/58709498