jeudi 20 août 2015

How can I stub an anonymous AMD module function with sinon api for javascript unit tests

I am using the sinon stub api to unit test some AMD based javascript code. I'd like to be able to stub an AMD module that returns an anonymous function. How can I do so?

My AMD module that returns an anonymous function that I'd like to stub later:

define(["test/module"], function (testModule) {

    return function (arg) {
       //I'd like to stub this function later
       return false;
    };
});

My unit testing file:

define(["moduleToStub"], function (moduleToStub) {
   //How can I stub this module to return or function as I'd like?
   //example:

   sinon.stub(moduleToStub).returns(true); //didn't work
   moduleToStub = sinon.stub().returns(true); //didn't work

});

Of course, in my actual files I have sinon and all of my dependencies included. This is simplified for posting purposes.

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire