node.js - 节点模块超测试 - 发布方法不起作用
问题描述
我在下面有这个测试来测试控制器/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});
}
);
};
知道我在这里做错了什么吗?我没有看到与网上有关它的帖子有什么不同。
解决方案
这是针对您的案例的集成测试,也许您忘记添加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
推荐阅读
- cluster-computing - 避免在集群环境中同时访问 infinispan 缓存
- python - ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。收到的完整形状:(10, 24)
- arrays - 减少“读取时”循环的处理时间
- javascript - TypeError:创建 JS 对象时 Object(...) 不是函数
- uitableview - UITableViewCell 在 iOS 15 上使用 SwiftUI 视图进行不正确的动态高度计算
- firebase - 如何获取有效的 Firebase 用户 JWT 令牌
- javascript - React Query - useInfiniteQuery,如何在获取下一页时避免组件刷新
- javascript - Django javascript使用id重定向
- netsuite - suitescript 1.0 有什么方法可以在客户端脚本(如 getFieldText)上运行服务器端功能?
- react-native - React Native 的 VSCode 自动完成扩展?