wil*_*n.w 2 unit-testing angularjs
我只是想知道如何测试这个handleAddClientBroadcast事件?
我有这样的导航服务:
angular.module("ruleManagement.services")
.factory('navigationService', function ($rootScope) {
var navigationService = {};
navigationService.prepForBroadcast = function() {
this.broadCastIsAddClientItem();
};
navigationService.broadCastIsAddClientItem = function() {
$rootScope.$broadcast('handleAddClientBroadcast');
};
return navigationService;
});
Run Code Online (Sandbox Code Playgroud)
我把这个导航服务注入到我的中,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);
});
Run Code Online (Sandbox Code Playgroud)
谢谢.
假设你正在使用Jasmine
spyOn($rootScope, '$broadcast').andCallThrough();
...
expect($rootScope.$broadcast).toHaveBeenCalledWith('eventName');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1558 次 |
| 最近记录: |