Using Sinon to stub chained Mongoose calls
问题 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({"id": someId}) .where("someBooleanProperty").equals(true) ... .exec(someCallback); I tried the following: var findOneStub = sinon.stub(mongoose.Model, "findOne"); sinon.stub(findOneStub, "exec").yields(someFakeParameter); to no avail, any suggestions? 回答1: I've solved it by doing the following: var mockFindOne = { where: function () { return