2014-11-14 64 views
1

我有一個方法叫$scope.openGroupInbox(),它的確做了$scope.init()和其他的東西。 我想測試是否已調用$scope.init()。在打電話之前,在測試中.toHaveBeenCalled()功能

it('call init', function() { 
    $scope.openGroupInbox(3); 
    expect($scope.init(3)).toHaveBeenCalled(); 
}); 

回答

5

: 所以我這樣做。您需要註冊一名間諜,以跟蹤對$scope.init()功能的呼叫。

你可以在你的beforeEach像下面這樣做。

beforeEach(function() { 
    spyOn($scope, 'init'); 
}); 

然後,當您的測試運行時,您應該正確地獲知是否已成功調用$scope.init()函數。

您還需要將您的expect更改爲:

expect($scope.init).toHaveBeenCalled(); 

expect($scope.init).toHaveBeenCalledWith(3); 

我希望這有助於。

相關問題