How to stub the instance method bark
of the following fictitious schema?
如何存根以下虚构架构的实例方法?
var dogSchema = mongoose.Schema({
// ...
});
dogSchema.methods = {
bark() { console.log('Woof!') },
};
For example, if I want to test the following function barkOne()
:
例如,如果我想测试以下函数barkOne():
function barkOne() {
Dog.findOne().exec().then(dog => dog.bark());
}
How would I be able to stub it, in order to test it like this?
我怎么能将它存根,以便像这样测试它?
describe('barkOne', () =>
it('should make all dogs bark', () => {
barkOne().then(() => {
assert(barkStub.calledOnce);
});
})
});
Thanks!
1 个解决方案
#1
0
As of mongoose 4.4.5, I was able to stub methods with Model.prototype
. e.g.
从mongoose 4.4.5开始,我能够使用Model.prototype来存根方法。例如
const stub = sandbox.stub(Dog.prototype, 'bark');
Dog.findOne().exec().then(dog => {
// dog[0].bark === stub
})
#1
0
As of mongoose 4.4.5, I was able to stub methods with Model.prototype
. e.g.
从mongoose 4.4.5开始,我能够使用Model.prototype来存根方法。例如
const stub = sandbox.stub(Dog.prototype, 'bark');
Dog.findOne().exec().then(dog => {
// dog[0].bark === stub
})