使用Sinon来残存mongoose模型
我想存根mongoose模型返回一个json值
我有的代码是
var valueToReturn = { name:'xxxxx' }; var stub = sinon.stub(MyModel.prototype,'findOne'); stub.returns(valueToReturn);
我得到这个错误:TypeError:试图包装未定义的属性findOne作为函数
看一下sinon-mongoose 。 你可以期待只用几行链接的方法:
// If you are using callbacks, use yields so your callback will be called sinon.mock(YourModel) .expects('findById').withArgs('abc123') .chain('exec') .yields(someError, someResult); // If you are using Promises, use 'resolves' (using sinon-as-promised npm) sinon.mock(YourModel) .expects('findById').withArgs('abc123') .chain('exec') .resolves(someResult);
你可以找回工作的例子。
另外,build议:使用mock
方法而不是stub
,这将检查原始对象上真正存在的方法。