Testing a $rootScope broadcast event with Jasmine

Feb 23rd, 2015 in Javascript by Michael Cho

← All articles


A short snippet for testing AngularJS with Jasmine.

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

    $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', () ->
        expect(scope.superSecretValues).toEqual(sampleData)