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()
:
我想监视$ scope.addLeftRange(),这样当调用$ scope.addRangesAndSquare时,$ 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 个解决方案
#1
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与jasmine一起使用时,它会对该函数进行模拟,并且实际上并不执行任何内容。如果你想在其中测试更多的函数调用,你需要调用.andCallThrough(),如下所示:
spyOn($scope, 'addRangesAndSquare').andCallThrough();
that should do it.
应该这样做。
#1
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与jasmine一起使用时,它会对该函数进行模拟,并且实际上并不执行任何内容。如果你想在其中测试更多的函数调用,你需要调用.andCallThrough(),如下所示:
spyOn($scope, 'addRangesAndSquare').andCallThrough();
that should do it.
应该这样做。