如何存根 Mongoose 模型中的方法?
How to stub methods in a Mongoose model?
如何存根以下虚构模式的实例方法bark
?
var dogSchema = mongoose.Schema({
// ...
});
dogSchema.methods = {
bark() { console.log('Woof!') },
};
例如,如果我想测试下面的函数barkOne()
:
function barkOne() {
Dog.findOne().exec().then(dog => dog.bark());
}
我怎样才能对它进行存根,以便像这样对其进行测试?
describe('barkOne', () =>
it('should make all dogs bark', () => {
barkOne().then(() => {
assert(barkStub.calledOnce);
});
})
});
谢谢!
自 mongoose 4.4.5 起,我可以使用 Model.prototype
存根方法。例如
const stub = sandbox.stub(Dog.prototype, 'bark');
Dog.findOne().exec().then(dog => {
// dog[0].bark === stub
})
如何存根以下虚构模式的实例方法bark
?
var dogSchema = mongoose.Schema({
// ...
});
dogSchema.methods = {
bark() { console.log('Woof!') },
};
例如,如果我想测试下面的函数barkOne()
:
function barkOne() {
Dog.findOne().exec().then(dog => dog.bark());
}
我怎样才能对它进行存根,以便像这样对其进行测试?
describe('barkOne', () =>
it('should make all dogs bark', () => {
barkOne().then(() => {
assert(barkStub.calledOnce);
});
})
});
谢谢!
自 mongoose 4.4.5 起,我可以使用 Model.prototype
存根方法。例如
const stub = sandbox.stub(Dog.prototype, 'bark');
Dog.findOne().exec().then(dog => {
// dog[0].bark === stub
})