Jasmine - 如何监视函数中的函数调用?
以下在我的控制器中:
$scope.addRangesAndSquare = function() {
$scope.addLeftRange();
$scope.addCenterSquare();
$scope.addRightRange();
}
我想监视 $scope.addLeftRange()
,所以当 $scope.addRangesAndSquare
被调用时 $scope.addLeftRange()
:
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();
});
如何做到这一点?
推荐答案
默认情况下,当您将 spyOn
与 jasmine 一起使用时,它会模拟该函数而实际上并没有执行其中的任何内容.如果你想测试更多的函数调用,你需要调用 .andCallThrough()
,像这样:
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();
应该这样做.
相关文章