Using Sinon to stub chained Mongoose calls

前端 未结 5 1492
感情败类
感情败类 2020-12-31 10:34

I get how to stub Mongoose models (thanks to Stubbing a Mongoose model with Sinon), but I don\'t quite understand how to stub calls like:

myModel.findOne({\"         


        
5条回答
  •  Happy的楠姐
    2020-12-31 10:42

    I use promises with Mongoose and stub the methods like this:

    const stub = sinon.stub(YourModel, 'findById').returns({
        populate: sinon.stub().resolves(document)
    })
    

    Then I can call it like:

    const document = await YourModel.findById.populate('whatever');
    

提交回复
热议问题