以下在我的控制器中:
$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();
应该这样做.
这篇关于Jasmine - 如何监视函数中的函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 javascript 认为文档“准备好"之前,如何让How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 认为文档“准备好
jasmine 运行和等待实际上是做什么的?What do jasmine runs and waitsFor actually do?(jasmine 运行和等待实际上是做什么的?)
如何提供模拟文件来更改 <input type='filHow to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模拟文件来更改 lt;input type=filegt; 的事
如何使用 Jasmine 对链式方法进行单元测试How to unit test a chained method using Jasmine(如何使用 Jasmine 对链式方法进行单元测试)
如何将 $rootScope 注入 AngularJS 单元测试?How do I inject $rootScope into an AngularJS unit test?(如何将 $rootScope 注入 AngularJS 单元测试?)
茉莉花测试失败,未定义不是函数(评估 $browserjasmine test fails with undefined is not a function(evaluating $browser.$$checkUrlChange())(茉莉花测试失败,未定义不是函数(评估 $brows