【发布时间】:2017-03-27 18:41:19
【问题描述】:
我正在尝试使用 Sinon.js 来存根我的 Student Mongoose 模型的模型构造函数。
var Student = require('../models/student');
var student = new Student({ name: 'test student' }); // I want to stub this constructor
查看Mongoose源代码,Model从Document继承其原型,它调用Document函数,所以这是我为了存根构造函数而尝试的。但是,我的存根永远不会被调用。
sinon.stub(Student.prototype__proto__, 'constructor', () => {
console.log('This does not work!');
return { name: 'test student' };
});
createStudent(); // Doesn't print anything
感谢您的任何见解。
编辑:
我不能直接将Student() 设置为存根,因为我还在另一个测试中存根Student.find()。所以我的问题本质上是“我如何同时存根Student() 和Student.find()?”
【问题讨论】:
标签: javascript node.js mongodb mongoose sinon