26

The following is in my controller:

$scope.addRangesAndSquare = function() {
    $scope.addLeftRange();
    $scope.addCenterSquare();
    $scope.addRightRange();
}

And I want to spy on $scope.addLeftRange(), so that when $scope.addRangesAndSquare is called so is $scope.addLeftRange():

it('expect addLeftRange to be called after calling addRangesAndSquare', function () {
    spyOn(scope ,'addRangesAndSquare');
    spyOn(scope, 'addLeftRange');
    scope.addRangesAndSquare();
    expect(scope.addLeftRange).toHaveBeenCalled();
});

How can this be done?

thanksd
  • 54,176
  • 22
  • 157
  • 150
criver.to
  • 520
  • 2
  • 9
  • 18

1 Answers1

42

By default, when you use spyOn with jasmine, it mocks that function and doesn't actually execute anything within it. If you want to test further function calls within, you'll need to call .andCallThrough(), like so:

spyOn($scope, 'addRangesAndSquare').andCallThrough();

that should do it.

Paritosh
  • 11,144
  • 5
  • 56
  • 74
UnicodeSnowman
  • 1,639
  • 16
  • 13
  • Thanks. Also, I had to comment out or add a spy to the other two inner functions, otherwise it will throw out an error. – criver.to Jul 17 '13 at 17:53
  • 31
    In Jasmine 2, it has been changed to `spyOn($scope, 'addRangesAndSquare').and.callThrough();` – fracz Jan 05 '15 at 10:25