【发布时间】:2020-09-12 02:07:14
【问题描述】:
学习如何使用 Mocha、Chai、Chai-HTTP 插件和 MongoDB进行 Express 测试> 与猫鼬。我有一个测试来故意检测 MongoDB 是否会在尝试使用错误的 _id 值(太短)查找文档时发回错误。
我注意到我的部分代码在我的其他 Express 路线周围重复,并希望将其重用于其他路线,所以我从另一个模块导出它,但现在我得到了:
Uncaught Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
不知道为什么我会收到此错误。如果我有与导出函数相同的代码,在路由代码中它工作正常,但导出它只是抱怨。
代码如下:
test/route/example.test.js Snippit
it('Bad request with a too short ID string (12 characters minimum)', () => {
// /api/v1/example is the endpoint
// /blah is the param
chai.request(app).get('/api/v1/example/blah').end((err, res) => {
// Insert all the nice assert stuff. :)
});
});
route/example.js片段
// Packages
const router = require('express').Router();
// Models (Mongoose Schemas)
const Example = require('../models/example.model');
// Helpers
const { foundMongoError } = require('../helpers/routes');
// -----Snipped-----
router.route('/:exampleId').get((req, res) => {
// Retrieve the exampleId parameter.
const exampleId = req.params.exampleId;
Example.findById(exampleId, (mongoError, mongoResponse) => {
foundMongoError(mongoError, res); // Having an issue
// If I have the same code that makes up foundMongoError inside here, no issues,
// but it will no longer be DRY.
// Check if any responses from MongoDB
if(mongoResponse) {
res.status(200).json(mongoResponse);
} else {
return res.status(404).json({
errorCode: 404,
errorCodeMessage: 'Not Found',
errorMessage: `Unable to find example with id: ${exampleId}.`
});
}
});
});
helpers/routes.js
const foundMongoError = (mongoError, res) => {
if(mongoError) {
return res.status(400).json({
errorCode: 400,
errorCodeMessage: 'Bad Request',
errorMessage: mongoError.message
});
}
};
module.exports = {
foundMongoError
};
【问题讨论】:
标签: javascript node.js express mongoose mocha.js