我在我的控制器中测试一个具有$ broadcast事件的函数。我正在使用Jasmine 2.1.3和视觉工作室进行测试运行。我相信我正在监视$ scope。$正确播放但我一直收到这个错误:
Expected spy $broadcast to have been called.
我有一个内置此功能的控制器:
vm.doSomething = function() {
var things = $scope.things;
for(var i = 0; i < things.length; i++){
if(things[i] == true) {
$scope.$broadcast('isTrue', things[i]);
}
}
}
在我的测试文件中:
beforeEach(function () {
module('app');
inject(function (_$controller_, _$rootScope_) {
$scope = _$rootScope_.$new();
$controller = _$controller_;
spyOn($scope, '$broadcast').and.callThrough();
myController = $controller('myController',{ $scope: $scope });
})
})
it('should $broadcast event when vm.doSomething is called', function () {
myController.doSomething();
$scope.$apply();
expect($scope.$broadcast).toHaveBeenCalled();
});
我已经阅读了很多帖子并尝试了所有解决方案,但似乎都没有。