I have a simple express routine to save incoming data:
app.post('/news', async (req, res, next) => {
const news = await model.create(req.body);
res.status(201).json(news);
});
And I want to write unit test for this by using mockingoose
and supertest
.
it('should create news', async () => {
mockingoose(NesModel).toReturn({ _id: '507f191e810c19729de860ea', title: 'dadsd' }, 'save');
const res = await request(app)
.post('/news')
.send({
title: 'ABC',
content: 'adbasdasdsad'
});
expect(res.statusCode).toBe(201);
});
And I get this error:
● News › should create news
ValidationError: News validation failed: _id: Cannot read property 'where' of undefined
at new ValidationError (node_modules/mongoose/lib/error/validation.js:31:11)
at model.Document.invalidate (node_modules/mongoose/lib/document.js:2563:32)
at node_modules/mongoose/lib/document.js:2385:17
at node_modules/mongoose/lib/schematype.js:1181:9
I couldn't figure out how to fix this. Any suggestion?