← All Articles

Testing a $rootScope broadcast event 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)

Made with JoyBird