Using Sinon to stub chained Mongoose calls

前端 未结 5 1491
感情败类
感情败类 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条回答
  •  难免孤独
    2020-12-31 10:56

    Another way is to stub or spy the prototype functions of the created Query (using sinon):

    const mongoose = require('mongoose');
    
    sinon.spy(mongoose.Query.prototype, 'where');
    sinon.spy(mongoose.Query.prototype, 'equals');
    const query_result = [];
    sinon.stub(mongoose.Query.prototype, 'exec').yieldsAsync(null, query_result);
    

提交回复
热议问题