vendredi 29 mai 2015

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 the ui.headerMessage to empty', function() {
        spyOn(cache, 'set');
        expect(cache.set).toHaveBeenCalledWith('ui', 'headerMessage', '');
    });

});

So I expect cache.set to be called, but instead I get Expected spy set to have been called with [ 'ui', 'headerMessage', '' ] but it was never called.

What am I doing incorrectly?

Aucun commentaire:

Enregistrer un commentaire