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.
 
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)


Other articles you may like

Introduction to Client-Side Javascript in 2018
Sep 30th, 2018
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