如何测试一个事件已AngularJS已经播出? [英] How do I test an event has been broadcast in AngularJS?
本文介绍了如何测试一个事件已AngularJS已经播出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道我怎么可以测试 handleAddClientBroadcast
事件?
I am just wondering how can I test the handleAddClientBroadcast
event?
我有一个导航服务,像这样:
I have a navigation service like so:
angular.module("ruleManagement.services")
.factory('navigationService', function ($rootScope) {
var navigationService = {};
navigationService.prepForBroadcast = function() {
this.broadCastIsAddClientItem();
};
navigationService.broadCastIsAddClientItem = function() {
$rootScope.$broadcast('handleAddClientBroadcast');
};
return navigationService;
});
我注入这个导航服务到我的 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);
});
感谢。
推荐答案
假设你正在使用茉莉花
spyOn($rootScope, '$broadcast').andCallThrough();
...
expect($rootScope.$broadcast).toHaveBeenCalledWith('eventName');
这篇关于如何测试一个事件已AngularJS已经播出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文