如何模拟返回promise karma单元测试的私有函数

时间:2018-04-05 11:05:27

标签: karma-runner karma-jasmine

我想在工厂内测试一个函数(publicFunction)(TestFactory)。此函数调用私有函数(privateFunction),该函数在另一个工厂(callerfactory)内返回带有调用和其他函数(调用者)的promise。我如何模拟私有函数?



module.factory('TestFactory',['callerfactory','$q',function($q,callerfactory){


  function privateFunction(varr){

   var deferred = $q.defer( );
   var data = new Caller(varr);
   varr.init().then( function( ) {
        deferred.resolve( vari );         //This is important to be resolved
      } );

      return deferred.promise;
   }
   
   
  this.publicFunction = function(list){
  
   var promises = [];
  
   angular.forEach(list.find('ind'), function (sampleVar,index) {
  
    promises.push( privateFunction(sampleVar));
   }
   $q.all(promises).then ( function (status) ) {
  
    Varriedlist = status;                //I want to test if this list whould be filled
    deferred.resolve( true );
   }
  
   return deferred.promise;
   }
} ] );




来电者事实:



module.factory( 'callerfactory', function() {

  var caller = function (varToCall) {
  
    var _varToCall= this;
  
    this.init = function () {
       var deferred = $q.defer( );
   
       //some implementations
     
       return deferred.promise;
    }
  }
  return caller;
}




我正在使用karma进行单元测试和jasmine 2框架。

0 个答案:

没有答案