2016-08-01 67 views
0

我怎么能嘲笑和单元测试使用因缘/茉莉

 
I have an angular factory which again calls another factory- service 
that uses $resource for API call. 
Could someone tell me, How can I mock and test this kind of constructs. 

//factory 
    angular.module('myapp') 
     .factory('SampleF', function (SampleS) { 
      return { 
       getData: function (parm, callback) { 
        var cb = callback || angular.noop; 

        return SampleS.get(parm, 
         function (res) { 
          return cb(res); 
         }, 
         function (err) { 
          return cb(err); 
         }.bind(this)).$promise; 
       } 
      }; 
     }); 


// Service: SampleS 

     angular.module('myapp') 
     .factory('SampleS', function ($resource) { 
      return $resource('http://localhost:8080:/api/sample/:parm', { 
       parm: '@parm', 
      }, {}); 
     }); 

//API response will be 
`   { 
      "firstName": "John", 
      "lastName": "Franklin", 
      "companyName": "Benton, John B Jr", 
      "address": "6649 N Blue Gum St", 
      "city": "New Oakland", 
      "county": "Oakland", 
      "statie": "LA", 
      "zip": "703333", 
      "phone": "503-321-2227", 
      "phone2": "514-145-427", 
      "email": "[email protected]", 
     } 

回答

0
describe('test',function(){ 

    beforeEach(module('myapp')); 
    var sampleF,myapp, 
    beforeEach(inject(function(_sampleF_,_myapp_){ 
     sampleF=_sampleF_; 
     myapp=_myapp_; 
    })); 
    it('testing'function(){ 
     //here you can call your function and expect the values 
    }); 
}); 
以下AngularJS工厂