I'm trying to spy on an $emit from a directive, but somehow I cannot get the spy to 'hear' the $emit.
This is the code in my directives' controller:
$scope.$on('send', function () {
console.log('called');
$scope.$emit('resultSend', {'ok': true, 'data': ''});
});
This is my unit test:
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');
});
The console.log output ('called') is printed out by Karma, so I guess the unit test broadcast event does work.
Does this have to do with $emit not broadcasting down but up, and if so, how do I catch it, and if not, how else do I handle this case?