24 February 2015

Testing a $rootScope broadcast event with Jasmine

<p>A short snippet for testing AngularJS with Jasmine.</p>

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)

© 2026 Michael Cho