gpt4 book ai didi

AngularJS 测试 $scope 方法是否被调用

转载 作者:行者123 更新时间:2023-12-02 23:04:39 25 4
gpt4 key购买 nike

我有一个名为 $scope.openGroupInbox() 的方法,它执行 $scope.init() 和其他操作。我想测试是否已调用 $scope.init() 。所以我正在这样做:

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

最佳答案

在测试中调用 .toHaveBeenCalled() 函数之前。您需要注册一个 spy 来跟踪对 $scope.init() 函数的调用。

您可以在 beforeEach 中执行此操作,如下所示。

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

然后,当您的测试运行时,您应该正确地获知 $scope.init() 函数是否已成功调用。

您还需要将您的期望更改为:

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

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

我希望这会有所帮助。

关于AngularJS 测试 $scope 方法是否被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26934828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com