jeudi 14 mars 2019

Mocking mongoose.save() to resolve `this` when calling `this.save()`

I'm trying to write unit tests for our application which uses mongoose. I have instance methods on a model which call this.save()

Eg.

MyModel.methods.update = function(data) {
    this.param = data
    this.save().then(updatedModel => {
        return updatedModel
    })
}

Is there a way to stub mongoose save to return the current this object?

Essentially, something like this:

const save = sinon.stub(MyModel.prototype, 'save').resolves(this);

but this is the referring to this in the instance method.

Hopefully what I described makes sense. Any help is appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire