Here's a function I've been working with:
$scope.doIt = function( x, y )
{
$timeout( function ()
{
$rootScope.$broadcast( 'xxx',
{
message: xxx,
status: xxx
} );
} );
}
The function appears to be functioning correctly. However, when I tried to write a test for it, I encountered some difficulties.
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 );
} ) );
}
);
This resulted in the following error:
TypeError: 'undefined' is not an object (evaluating '$rootScope.$broadcast('$locationChangeStart', newUrl, oldUrl, $location.$$state, oldState).defaultPrevented')
Why is this happening? What am I doing wrong? The function and test work fine without $timeout.
Thank you in advance for any assistance.
:)
Edit: Another broadcast seems to be causing this issue. Hmm