2014-11-14 38 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); 

我希望这有助于。

相关问题