vendredi 26 août 2016

How to mock an external dependency method callback params in Nodejs?

Let's say that I have the following:

lib/modules/module1.js

var m2 = require('module2');

module.exports = function(){
    return {
        // ...
        get: function(cb){
            m2.someMethod(params, function(error, data){
                if(error){
                    cb(error)
                }

                cb(null, data)
            })
        },
        // ...
    }
}

Now let's say that I have a set of tests in another dir, e.g. tests/testModule1.js. From this file, I create an instance of module1 to peform some tests.

I would like to mock the objects passed by the m2.someMethod to it's callback function (not the cb function), from the file testModule1.js.

I've looked into Sinon.js, but I couldn't figure a way to do this. Actually, I that even possible?

Thanks.

Aucun commentaire:

Enregistrer un commentaire