title: angular jasmine: 'undefined' is not an object - broadcast within timeout - error
body: <p>I have a function like this:</p>
  
  <pre><code> $scope.doIt = function( x, y )
   {
     $timeout( function ()
     {
       $rootScope.$broadcast( 'xxx',
       {
          message: xxx,
          status: xxx
       } );
     } ); 
   }
  </code></pre>
  
  <p>This function works fine so far. But while writing a test I had some trouble. </p>
  
  <pre><code>describe( 'test doIt function...', function ()
        {
          var $rootScope, $timeout;
  
          beforeEach( inject( function ( _$rootScope_, _$timeout_ )
          {
            $rootScope = _$rootScope_;
            $timeout = _$timeout_;
            spyOn( $rootScope, '$broadcast' );
            spyOn( scope, 'doIt' ).and.callThrough();
          } ) );
  
          it( 'test broadcast will be called', inject( function ()
          {
            var testObj = {
              message: 'test1',
              status: 'test2'
            };
  
            scope.doIt( 'test1', 'test2' );
  
            expect( $rootScope.$broadcast ).not.toHaveBeenCalledWith( 'xxx', testObj );
  
            $timeout.flush();
  
            expect( $rootScope.$broadcast ).toHaveBeenCalledWith( 'xxx', testObj );
  
          } ) );
        }
      );
  </code></pre>
  
  <p>This will end up in the following error:</p>
  
  <blockquote>
    <p>TypeError: 'undefined' is not an object (evaluating
    '$rootScope.$broadcast('$locationChangeStart', newUrl, oldUrl,
                    $location.$$state, oldState).defaultPrevented')</p>
  </blockquote>
  
  <p>Why? What I am doing wrong? 
  Without $timeout in function and test it works fine. </p>
  
  <p>Thanks in advance for your help. </p>
  
  <p>:) </p>
  
  <p>Edit: Another broadcast then expected is issuing this issue. hmm</p>