angularjs - function is not being called with a karma spyOn -


my controller is:

window.map.controller('schedulehomecontroller', ['$scope', '$location', '$route', '$routeparams', 'cache', 'patientcache',     function($scope, $location, $route, $routeparams, cache, patientcache) {          cache.set('ui', 'headermessage', '');          if(cache.get('patient', 'currentpatient').patientid !== $routeparams.patientid) {             cache.set('patient', 'currentpatient', patientcache.getpatient(parseint($routeparams.patientid, 10)));         }          switch($route.current.originalpath) {             case '/patients/:patientid/schedule':                 cache.set('ui', 'schedulepage', 'daily-schedule');                 $scope.showschedulenavbar = true;                 break;             default:                 $scope.showschedulenavbar = false;                 break;         }     } ]); 

my test is:

fdescribe('schedulehomecontroller', function() {     var scope, cache, $route, patientcache, $routeparams;      beforeeach(function() {       module('mapapp');        return inject(function($injector) {         var $controller, $rootscope;         $rootscope = $injector.get('$rootscope');         $controller = $injector.get('$controller');         $route = $injector.get('$route');         cache = $injector.get('cache');         patientcache = $injector.get('patientcache');         $routeparams = $injector.get('$routeparams');          scope = $rootscope.$new()         $route.current = { originalpath: '/patients/:patientid/schedule' };          $controller('schedulehomecontroller', {             $scope: scope,             cache: cache,             patientcache: patientcache,             $route: $route,             $routeparams: $routeparams         });          return scope.$digest();        });     });      it('should set ui.headermessage empty', function() {         spyon(cache, 'set');         expect(cache.set).tohavebeencalledwith('ui', 'headermessage', '');     });  }); 

so expect cache.set called, instead expected spy set have been called [ 'ui', 'headermessage', '' ] never called.

what doing incorrectly?

you have spyon cache.set before function execute (in controllers init). put before $contorller:

... spyon(cache, 'set'); $controller('schedulehomecontroller', { ... 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -