首页 > 解决方案 > 节点模块超测试 - 发布方法不起作用

问题描述

我在下面有这个测试来测试控制器/post 端点,但是当我运行这个测试时,它一直抱怨缺少字段。我在.send()方法中提供了正确的数据,但由于某种原因它无法检测到它并说该name字段丢失。

    describe("/POST a book", () => {
        it("should save a book", async () => {
            var book_data = { name: "book_title" };
            const res = await request(app).post("/books")
            .send(book_data)
            .expect(201)
            .end(function(err, res) {
              if (err) return done(err);
              done();
            });
        });
    });

路由.js

 app.post('/books', [bookController.create_a_book]);

bookController.js

exports.create_a_book = function (req, res) {
    BookModel.createBook(req.body).then(
        (book) => {
            res.status(200).send({id: book._id});
        }
    );
};

知道我在这里做错了什么吗?我没有看到与网上有关它的帖子有什么不同。

标签: node.jsunit-testingtddsupertest

解决方案


这是针对您的案例的集成测试,也许您忘记添加body-parser中间件。

server.js

const express = require("express");
const bookController = require("./bookController");
const http = require("http");
const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.json());
app.post("/books", [bookController.create_a_book]);

const server = http.createServer(app).listen(3000, () => {
  console.log("HTTP server is listening on http://localhost:3000");
});

module.exports = server;

bookController.js

const BookModel = require("./bookModel");

exports.create_a_book = function(req, res) {
  BookModel.createBook(req.body).then((book) => {
    console.log("book: ", book);
    res.status(200).send({ id: book._id });
  });
};

bookModel.js

module.exports = {
  async createBook(data) {
    data._id = Math.random();
    return data;
  },
};

server.integration.spec.js

const request = require("supertest");
const { expect } = require("chai");
const app = require("./server");

describe("/POST a book", () => {
  after((done) => {
    app.close(done);
  });
  it("should save a book", (done) => {
    const book_data = { name: "book_title" };
    request(app)
      .post("/books")
      .send(book_data)
      .expect(200)
      .end(function(err, res) {
        console.log(res.body);
        if (err) return done(err);
        expect(res.body).to.haveOwnProperty("id");
        done();
      });
  });
});

带有覆盖率报告的集成测试结果:

HTTP server is listening on http://localhost:3000
  /POST a book
book:  { name: 'book_title', _id: 0.2510749824531193 }
{ id: 0.2510749824531193 }
    ✓ should save a book (497ms)


  1 passing (502ms)

----------------------------|----------|----------|----------|----------|-------------------|
File                        |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------------------|----------|----------|----------|----------|-------------------|
All files                   |    96.88 |       50 |      100 |      100 |                   |
 bookController.js          |      100 |      100 |      100 |      100 |                   |
 bookModel.js               |      100 |      100 |      100 |      100 |                   |
 server.integration.spec.js |    92.86 |       50 |      100 |      100 |                18 |
 server.js                  |      100 |      100 |      100 |      100 |                   |
----------------------------|----------|----------|----------|----------|-------------------|

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


推荐阅读