Reputation: 2106
I am trying to stub a mongoose model to return a json value
the code that i have is
var valueToReturn = {
name:'xxxxx'
};
var stub = sinon.stub(MyModel.prototype,'findOne');
stub.returns(valueToReturn);
I get this error : TypeError:Attempted to wrap undefined property findOne as function
Upvotes: 1
Views: 1975
Reputation: 627
Take a look to sinon-mongoose. You can expects chained methods with just a few lines:
// 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);
You can find working examples on the repo.
Also, a recommendation: use mock
method instead of stub
, that will check the method really exists on the original object.
Upvotes: 3