javascript - How do I test that this nested function was called in an AngularJS unit test? -
i'm not new unit testing (c#) new unit testing in angularjs. i'm trying test controller , far have been able several tests work properly, there proving rather difficult.
i have $scope method makes call our authentication service returns promise. in "then" function checking see if user indeed authenticated , based on call private function go out , make other service calls.
currently test failing following error:
expected spy getconfigurationstatuses have been called. error: expected spy getconfigurationstatuses have been called.
if can please point me in right direction appreciate it. i'll post code below -thanks help!
here's specs (the 1 not working "should call specific configurations if user authenticated" spec:
describe('environmentctrl specs', function(){ var $rootscope = null, $scope = null, ctrl = null; var authentication = { getcredentials: function(){ return true; } }; var environment = { getconfigurationstatuses: function(){ return true; } }; beforeeach(module('ngroute')); beforeeach(module('environment')); beforeeach(module(function($provide){ $provide.value('site_root', '/'); })); beforeeach(inject(function(_$rootscope_, _$controller_, _$timeout_, _$location_, _$q_, _authentication_, _environment_){ $rootscope = _$rootscope_; $controller = _$controller_; $timeout = _$timeout_; $location = _$location_; $q = _$q_; authentication = _authentication_; environment = _environment_; spyon(authentication, 'getcredentials').andcallthrough(); spyon(environment, 'getconfigurationstatuses').andcallthrough(); $rootscope = _$rootscope_; $scope = $rootscope.$new(); ctrl = $controller('environmentctrl', {$rootscope: $rootscope,$scope: $scope, $timeout: $timeout, eventor: {}, controller: {}, environment: environment,authentication: authentication, errorservice:{} }); })); describe('when initializing environmentctrl', function(){ // 1 works fine! it('should set default values on scope object', function(){ expect($scope.controllername).toequal('environmentctrl'); expect($scope.environmentstatustype).toequal('configurations'); expect($scope.configurationsselected).tobe(true); expect($scope.isdataloaded).tobe(false); }); // works fine! it('should make call authenticate user', function(){ $scope.determineviewtodisplay(); expect(authentication.getcredentials).tohavebeencalled(); }); // 1 doesn't work! it('should call specific configurations if user authenticated', function(){ $scope.determineviewtodisplay(); $rootscope.isuserauthenticated = true; expect(environment.getconfigurationstatuses).tohavebeencalled(); }); }); });
here's 3 functions involved in unit tests:
$scope.determineviewtodisplay = function () { authentication.getcredentials().then(function(){ if ($rootscope.isuserauthenticated === true) { $scope.isanonymous = false; handleauthenticateduserview(); } else { eventor.publish('event:login', false); $scope.isanonymous = true; handleanonymoususerview(); } }, function(err){ errorservice.handleerror(err, null, $scope.controllername); }); }; function handleauthenticateduserview() { $scope.configurationstatustimer = $timeout(function(){ displayconfigurationstatuses(true); }, 5); } function displayconfigurationstatuses(isauthenticated) { environment.getconfigurationstatuses(isauthenticated).then(function(statuses){ setconfigurationsiconstatus(statuses); $scope.configurationstatuses = statuses; $scope.isdataloaded = true; amplify.store($rootscope.productcustomername + '-configurationstatuses', statuses, {expires: 120000}); $rootscope.showloadingindicator = false; }, function(err){ errorservice.handleerror(err, null, $scope.controllername); }); }
it looks determineviewtodisplay() calls handleauthenticateduserview if
$rootscope.isuserauthenticated === true
but you're not setting $rootscope.isuserauthenticated true until after call
$scope.determineviewtodisplay()
so handleauthenticateduserview() never gets called, , in turn displayconfigurationstatuses() never gets called.
Comments
Post a Comment