Testing a $rootScope broadcast event with Jasmine

Feb 23rd, 2015 in  by Michael Cho

A short snippet for testing AngularJS with Jasmine.

Assuming you have an AngularJS controller where something happens based on a broadcasted event.

    $rootScope.$on 'rootScope:myBigEvent', (event, myNewValues) ->
      $scope.superSecretValues = myNewValues


You can test this in your Jasmine test by using:

    describe 'rootScope:myBigEvent', () ->
      beforeEach () ->
        $injector.get('$rootScope').$broadcast('rootScope:myBigEvent', sampleData)
      it 'should set superSecretValues to broadcast value', () ->

Other articles you may like

Supercharged Javascript and CSS animations
Apr 21st, 2015
Overview of basic terms in AngularJS
Mar 25th, 2015
Should I use AngularJS or jQuery?
Jan 30th, 2015