我有一个名为$scope.openGroupInbox()
的方法可以执行$scope.init()
和其他内容。
我想测试是否已调用$scope.init()
。
所以我这样做:
it('call init', function () {
$scope.openGroupInbox(3);
expect($scope.init(3)).toHaveBeenCalled();
});
答案 0 :(得分:7)
在测试中调用.toHaveBeenCalled()
函数之前。您需要注册一个间谍,以跟踪对$scope.init()
功能的调用。
您可以在beforeEach
中执行此操作,如下所示。
beforeEach(function() {
spyOn($scope, 'init');
});
然后,当您的测试运行时,您应该正确地获知是否已成功调用$scope.init()
函数。
您还需要将expect
更改为:
expect($scope.init).toHaveBeenCalled();
或
expect($scope.init).toHaveBeenCalledWith(3);
我希望这会有所帮助。