Jasmine junit testing of delegate callback of function args

雨燕双飞 提交于 2019-12-12 02:49:59

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/26583283/jasmine-junit-testing-of-delegate-callback-of-function-args

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!