angularjs - Mocking a service and calling through in Jasmine -


i'm trying test service in angularjs project. i'm trying see if method on service has called through. thought when use 'and.callthrough()' in jasmine called method , can see if called. however, when test function karma giving me response of 'expected spy of getartists have been called'.

describe('practice', function(){  beforeeach(module('myapp'));  var ctrl, loadartists, rootscope, datafactory;  beforeeach(inject(function($controller, $rootscope, datafactory){     spyon(datafactory, 'getartists').and.callthrough();     datafactory = datafactory     rootscope = $rootscope;     scope = rootscope.$new();     ctrl = $controller('launchctrl',{scope: scope, artistspicsrotate: []}); }));  it('should nothing',function(){     expect(ctrl.artistspicsrotate).toequal([]) });  it('should call through datafactory', function(){     expect(datafactory.getartists).tohavebeencalled();     expect(datafactory.getartists.calls.count()).toequal(1); }); 

});

any ideas on why isn't working appreciated.

as explained in comments call service done ui-router resolves before controller instantiated. means controller never explicitly calls dataservice.getartists(), because call done when routing state resolved , received result injected in controller. when testing controller there no need test call service because call not directly made it.

however if want test state definition here 1 example how can it.

describe('practice', function(){   beforeeach(module('myapp'));   var ctrl, loadartists, rootscope, datafactory, $state;   beforeeach(inject(function($controller, $rootscope, datafactory, _$state_){     datafactory = datafactory     rootscope = $rootscope;     scope = rootscope.$new();     $state = _$state_;     ctrl = $controller('launchctrl',{scope: scope, artistspicsrotate: []});   }));    it('should nothing',function(){       expect(ctrl.artistspicsrotate).toequal([])   });    // note: test state definition   describe('state definition', function () {      var statedefinition;     beforeeach(function () {       statedefinition = $state.get('statename');     });      // note: write test each resolve     it('should resolve artists', function() {       // given       spyon(datafactory, 'getartists').and.callthrough();        // when       var artists = statedefinition.resolve.artistspicsrotate();        //       expect(artists).tobedefined();       expect(datafactory.getartists).tohavebeencalled();       expect(datafactory.getartists.calls.count()).toequal(1);     });     }); }); 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -