Stubbing a Mongoose model using Sinon

前端 未结 3 666
故里飘歌
故里飘歌 2020-12-16 05:11

I am trying to stub the mongoose dependency used in this object:

var Page = function(db) {

    var mongoose = db || require(\'mongoose\');

    if(!this ins         


        
3条回答
  •  -上瘾入骨i
    2020-12-16 06:01

    I've analysed mongoose source and don't think this is possible. Save function is not defined on model, but dynamically generated by hooks npm which enables pre/post middleware functionality.

    However, you can stub save on instance like this:

    page = new Page();
    sinon.stub(page, 'save', function(cb){ cb(null) })
    

    UPDATE: Stubbing out pageModel

    First, you need to make pageModel accessible by setting it as own property of Page (this.pageModel = xxx). Then, you can stub it like shown bellow:

    mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/test');
    mongoose.set('debug', true);
    
    schema = new mongoose.Schema({title: String});
    mongoose.model('Page', schema);
    
    
    var Page = function(db) {
    
      var mongoose = db || require('mongoose');
    
      if(!this instanceof Page) {
        return new Page(db);
      }
    
      var PageSchema = mongoose.model('Page');
      this.pageModel = new PageSchema();
    
      function save(params, cb) {
        console.log("page.save");
        this.pageModel.set(params);
        this.pageModel.save(function (err, product) {
          console.log("pageModel.save");
          cb(err, product);
        });
      }
    
      Page.prototype.save = save;
    };
    
    
    page = new Page();
    
    sinon = require('sinon');
    sinon.stub(page.pageModel, 'save', function(cb){
      cb("fake error", null);
    });
    
    page.save({ title: 'awesome' }, function (err, product) {
      if(err) return console.log("ERROR:", err);
      console.log("DONE");
    });
    

提交回复
热议问题