Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

node.js - Stubbing a Mongoose model using Sinon

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

var Page = function(db) {

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

    if(!this instanceof Page) {
        return new Page(db);
    }

    function save(params) {
        var PageSchema = mongoose.model('Page');

        var pageModel = new PageSchema({
            ...
        });

        pageModel.save();
    }

    Page.prototype.save = save;
}

module.exports = Page;

Using the answer for this question, I've tried doing this:

mongoose = require 'mongoose'
sinon.stub mongoose.Model, 'save'

But I got the error:

TypeError: Attempted to wrap undefined property save as function

I also tried this:

sinon.stub PageSchema.prototype, 'save'

And then I got the error:

TypeError: Should wrap property of object

Can anyone help with this? What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...