If you just need some kind of testing when starting and catching events, here's how I do it. To ensure that a specific event is fired ( $emit -ed or $broadcast -ed), a spy is the way to go. You need a reference to the area that will call $emit or $broadcast , and then just do something like this:
spyOn(scope, "$emit") //run code to test expect(scope.$emit).toHaveBeenCalledWith("MY_EVENT_ID", other, possible, args);
If you don't need or don't need to worry about the arguments passed with $emit , you can put $on in $rootScope and set a flag to know that the event was fired, Something like this:
var eventEmitted = false; $rootScope.$on("MY_EVENT_ID", function() { eventEmitted = true; });
For testing the functionality that is executed when an event is detected ( $on ), this is a bit easier. Just get $rootScope from the inject function and send the desired event.
$rootScope.$broadcast("EVENT_TO_TEST", other, possible, args); //expects for event here
Now I believe that this event handling will occur in a directive or controller (or both). To configure directory tests, see https://github.com/vojtajina/ng-directive-testing . To configure controller tests, see https://github.com/angular/angular-phonecat/blob/master/test/unit/controllersSpec.js#L27
Hope this helps.
dnc253 Mar 07 '13 at 22:22 2013-03-07 22:22
source share