本文介紹了Jasmine - 如何監視函數中的函數調用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
以下在我的控制器中:
$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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!