gpt4 book ai didi

unit-testing - 我如何测试一个事件已经在 AngularJS 中广播?

转载 作者:行者123 更新时间:2023-12-04 16:36:13 25 4
gpt4 key购买 nike

我只是想知道如何测试 handleAddClientBroadcast 事件?

我有这样的导航服务:

angular.module("ruleManagement.services")
.factory('navigationService', function ($rootScope) {

var navigationService = {};

navigationService.prepForBroadcast = function() {
this.broadCastIsAddClientItem();
};

navigationService.broadCastIsAddClientItem = function() {
$rootScope.$broadcast('handleAddClientBroadcast');
};

return navigationService;
});

我将此导航服务注入(inject)到我的 clientsCtrl 中并捕获 handleAddClientBroadcast,如下所示:

$scope.$on('handleAddClientBroadcast', function () {
$scope.clientModel = {
id: 0,
name: "",
description: "",
rules: []
};

var lastClient = _.findLast($scope.clients);

if (typeof lastClient == 'undefined' || lastClient == null) {
lastClient = $scope.clientModel;
}

$scope.clientModel.id = lastClient.id + 1;
$scope.clients.push($scope.clientModel);
});

谢谢。

最佳答案

假设您正在使用 Jasmine

spyOn($rootScope, '$broadcast').andCallThrough();

...

expect($rootScope.$broadcast).toHaveBeenCalledWith('eventName');

关于unit-testing - 我如何测试一个事件已经在 AngularJS 中广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19352636/

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