[英]Testing of express controllers where the access the mongoose model?
I want to unit test my express controllers independently. 我想独立测试我的Express控制器。 But they interact with the mongoose models. 但是它们与猫鼬模型相互作用。 So the question is how can I test them? 所以问题是我该如何测试它们? For example - 例如 -
const Model = require('./../models/mymodels');
const controller = (req, res) => {
const body = req.body;
const foo = new Model(body);
foo.save().then(foo => res.send(foo)).catch(err => res.send(error));
};
So I want to test the funtion controller
independently. 因此,我想独立测试功能controller
。
const expect = require('chai').expect;
const Model = require('./../models/mymodels');
describe('controller test', function() {
it('should be valid if body is correct', function(done) {
const testBody = {myKey: "myVal"}
const m = new Model(testBody);
m.validate(function(err) {
expect(err.errors).to.not.exist;
done();
});
});
});
This is an example, check the chai documents for more examples 这是一个示例,有关更多示例,请查看chai文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.