I am trying to run the $scope.$on() method $scope.$on() controller that fires when I have a $rootScope.broadcast() event. I found this question helpful: How to check events in angular? but I am still having trouble detecting an event being broadcast from $rootScope up to the $scope controller.
So far, I have been able to verify that the $broadcast method is called on the corresponding $rootScope , but not that the $on method is called on the corresponding $scope when $broadcast is called on $rootScope .
I tried $rootScope.$broadcast directly in my test, but my spy is not dialing an event.
This is my controller:
angular.module('app.admin.controllers.notes', []) .controller('NotesCtrl', function($scope) { $scope.$on('resource-loaded', function(event, resource) {
This is my test:
describe('The notes controller', function() { beforeEach(module('app.admin.controllers.notes')); var scope, rootScope, NotesCtrl; beforeEach(inject(function($controller, $injector, $rootScope) { rootScope = $rootScope; scope = $rootScope.$new(); // I've tried this with and without $new() NotesCtrl = $controller('NotesCtrl', {$scope: scope}); // I've tried explicitly defining $rootScope here })); it('should respond to the `resource-loaded` event', function() { spyOn(scope, '$on'); rootScope.$broadcast('resource-loaded'); // This is what I expect to trigger the `$on` method expect(scope.$on).toHaveBeenCalled(); }); });
And here is plunkr . I included passing the test of the $broadcast method for reference, mainly because I configured the tests in the same way.
I read quite a few questions related to event testing in AngularJS, and this always seems to be a problem. I heard that in Karma unit testing, $rootScope and $scope are the same thing, but I'm not quite sure what implication is. I tried to define $rootScope and $scope as the same object, and also explicitly insert $rootScope in NotesCtrl during testing, but nothing makes my test green.
How can I run the $on method in my NotesCtrl for this test?