Node.js 存根猫鼬查找方法
我正在尝试存根find或exec函数以测试以下函数:Node.js 存根猫鼬查找方法,node.js,unit-testing,mocha.js,sinon,Node.js,Unit Testing,Mocha.js,Sinon,我正在尝试存根find或exec函数以测试以下函数: function getOpenTickets() { return Ticket.find({}) .populate('assignees', ['fullName', 'firstName', 'email', 'notificationSettings.dailyEmail']) .populate('property', 'name') .populate('type', 't
function getOpenTickets() {
return Ticket.find({})
.populate('assignees', ['fullName', 'firstName', 'email', 'notificationSettings.dailyEmail'])
.populate('property', 'name')
.populate('type', 'title')
.populate({path: 'unit', model: 'Unit', select: 'title'})
.sort('created')
.lean()
.exec();
}
我找到了几篇关于存根猫鼬方法的帖子,但没有一篇对我有用,以下是我的:
it('should test getOpenTickets', async() => {
findStub = sinon.stub(Ticket, 'find');
var result = await utils.__get__('getOpenTickets')();
findStub.restore();
});
但我得到:
Cannot read property 'populate' of undefined
所以我试着用一个假物体来代替它:
var fakeFind = {
args: {
populate: [],
sort: null,
lean: null
},
populate: function (a) {
this.args.populate.push(a)
},
sort: function (a) {
this.args.sort = a;
},
lean: function () {
this.args.lean = true
},
exec: function () {
return Promise.resolve(this.args);
}
}
及
结果是:
TypeError: this.fakeFn.apply is not a function
我也尝试过用短截图来截取猫鼬、模型、原型、执行器和其他一些东西,但都没有成功
有什么想法吗?尝试使用sinon mongoose 以下是一个例子:
require('sinon');
要求(“sinon-mongoose”);
西农模拟(票)
.expects('find')
.chain('填充').withArgs(/*args*/)
.chain('sort')。withArgs('create'))
.chain(“精益”)
.chain('exec'))
.resolves('SOME_VALUE')代码>
TypeError: this.fakeFn.apply is not a function