我试图从指令中监视$ emit,但不知何故我无法让间谍“听到”$ emit。
这是代码在我的指令控制器:
$scope.$on('send', function() {
console.log('called');
$scope.$emit('resultSend', {'ok': true, 'data': ''});
});
这是我的单元测试:
var $rootScope, $compile, elm, element;
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
elm = angular.element('<test></test>');
element = $compile(elm)($rootScope);
}));
it('should listen for the send broadcast and emit the resultSend', function() {
spyOn($rootScope, '$emit');
$rootScope.$broadcast('send');
expect($rootScope.$emit).toHaveBeenCalledWith('resultSend');
});
的输出的console.log( '称为')通过噶打印出来,所以我猜测单元测试广播事件确实有效。
这是否与$发射没有广播下来,但如果是这样,如何捕捉它,如果不是,我该怎么处理这种情况?
您正在监视'$ rootScope'并调用'$ scope'上的'$ emit'。 –