Jasmine - How to spy on a function call within a f

2019-02-04 04:55发布

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?

1条回答
姐就是有狂的资本
2楼-- · 2019-02-04 05:35

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.

查看更多
登录 后发表回答