title: Testing a broadcast from a service, called from a controller
body: <p>In my Angular controller I make a call to a service to attach multiple events to a grid. For example, I attach an <code>onXLE</code> event to the grid like so:</p>
  
  <pre><code>gridService.attachEvent("visibilityGrid", "onXLE", function () {
          $rootScope.$broadcast("loader_hide");
      });
  </code></pre>
  
  <p>but I'm also attaching (using the same same function from the service) other events like <code>onCheck</code> and <code>onDynXLS</code>. The problem I'm having is that I can't seem to check if the <code>$broadcasts</code> are firing. I tried the usual approach:</p>
  
  <pre><code>it( 'should attach the onXLE event to the grid',function() {
     spyOn( gridService,'attachEvent' );
     spyOn( $rootScope,'$broadcast' );
  
     // First test passes
     expect( gridService.attachEvent ).toBeDefined();   
  
     // This fails...
     expect( $rootScope.$broadcast ).toHaveBeenCalledWith( 'loader_hide' );   
  </code></pre>
  
  <p>});</p>
  
  <p>It returns this error: </p>
  
  <pre><code>Expected spy $broadcast to have been called with [ 'loader_hide' ] but it was never called. 
  </code></pre>
  
  <p>What am I missing to make this test work?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The call to <code>$locationChangeStart</code> occurs here:</p>
  
  <pre><code>// For any route changes, check if roles have already been obtained, if not, then retrieve them.
      $scope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
  
          messageService.clearAllMessages("messageContainer");
  
          if(!authService.isInitialized()) {
  
              if(authService.isDevMode()){
  
                  dataService.getRoles(AUTH_APPLICATION_ID, function(response){
                      $scope.root.initAuthorization(response);
                  });
  
              } else {
  
                  dataService.retrieveSMHeaders()
                      .success(function(data, status, headers, config) {
  
                          authService.setEmployeeId(headers('EMPLOYEE_ID'));
                          authService.setEmployeeName(headers('USER_NAME'));
                          authService.setEmployeeSurname(headers('USER_SURNAME'));
  
                          // Set the Headers required to access Common Services.
                          $http.defaults.headers.common.SERVICES_ID = AUTH_SERVICES;
  
                          dataService.getRoles(AUTH_APPLICATION_ID, 
                              function(response){
                                  $scope.root.initAuthorization(response);
                              },
                              function(status){
                                  event.preventDefault();
                                  $location.url("/unauthorized");
                              }
                          );
  
                      });
              }
          }
  
          authService.getAuthPromise().then(function(){
  
              var isAuthorized = false;
              var userRoles = $route.current.$$route.roles;
              if(userRoles){
                  var userRoleArray = userRoles.split(',');
                  for (x=0; x&lt;userRoleArray.length; x++){
                      if(authService.hasRole(userRoleArray[x])){
                          isAuthorized = true;
                          break;
                      }
                  }
              } else {
                  isAuthorized = true;
              }
  
              if (!isAuthorized) {
                  event.preventDefault();
                  $location.url("/unauthorized");
              }
          });
      });
  </code></pre>
  
  <p>The point of this above is to determine if the user has the correct role to carry out the action - if not it obtains them</p>