Search code examples
javascriptnode.jsjunitjasminejasmine-node

Jasmine junit testing of delegate callback of function args


I have recently started using jasmine to write junit testcase for one of our application. I am stuck at this point on how to call the callBack function of the spied function.

setProfile :function(userProfile,callback){
        var user;
        var subjectInfo;

        iService.searchForAccess(subjectInfo , queryCalback);

        function queryCalback(err, userProfile) {
           if(err){
               callback(true,errorMessage)
           }else{
             callback(false,null)
           }
        }
}

Now in my spec i want to mock the call to iService.searchForAccess real world implementation and want to call nocallThrough for searchForAccess . but my queryCalback function has to be called for complete use case coverage.

In my spec i have tried to call queryCalback function explicitly by

spyOn(iService,'searchForAccess');
iService.searchForAccess.mostRecentCall.args[1](error, userProfile);

but iService.searchForAccess.mostRecentCall returns {}, empty object.

kindly help!!!!!!!!!!

Regards Punith


Solution

  • I have used sinonjs as solution to above problem statement. Below is the syntax of how its done.

    var sinon = require('../node_modules/sinon/lib/sinon.js');
    sinon.stub(iService, 'searchForAccess').callsArgWith(1, mockSubjectInfo, session.userProfile);
    

    Hope it will be helpfull to others.

    Regards Punith