我試圖從指令中監視$ 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'。 –