Shamoon
Shamoon

Reputation: 43491

My Karma spy is not picking up on my controller function

My spec:

describe('ScheduleController', function() {
    var ScheduleController, scope, spies = {};

    beforeEach(function() {
      module('mapApp');

      return inject(function($injector) {
        var $controller, $rootScope;
        $rootScope = $injector.get('$rootScope');
        $controller = $injector.get('$controller');

        scope = $rootScope.$new()

        $controller('ScheduleController', {
            $scope: scope
        });

        spies.buildScheduleUrl = spyOn(scope, 'buildScheduleUrl').and.callThrough();
      });
    });

    it('should build a schedule url', function() {
        expect(spies.buildScheduleUrl).toHaveBeenCalled();
    });
});

My controller:

window.map.controller('ScheduleController', ['$scope', '$window', 'cache', 'scheduleCache', 'dosingCache', 'patientCache', '$modal', 'util',
    function ($scope, $window, cache, scheduleCache, dosingCache, patientCache, $modal, util) {
        // other stuff here


        $scope.buildScheduleUrl();

    }

]);

So my buildScheduleUrl function does not get called it seems. What am I doing wrong?

Upvotes: 0

Views: 674

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

You're constructing the controller:

$controller('ScheduleController', {
     $scope: scope
});

which calls buildScheduleUrl() on the scope:

$scope.buildScheduleUrl();

and then, you spy on this function:

spies.buildScheduleUrl = spyOn(scope, 'buildScheduleUrl').and.callThrough();

So, obviously, there's no way for the spy to be aware of the call that has been made before it was created and began spying.

Upvotes: 1

Related Questions