我有一个这样的功能:
@H_502_2@$scope.doIt = function( x,y )
{
$timeout( function ()
{
$rootScope.$broadcast( 'xxx',{
message: xxx,status: xxx
} );
} );
}
到目前为止这个功能很好.但是在写测试的时候我有一些麻烦.
@H_502_2@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 ); } ) ); } );这将导致以下错误:
TypeError: ‘undefined’ is not an object (evaluating
‘$rootScope.$broadcast(‘$locationChangeStart’,newUrl,oldUrl,
$location.$$state,oldState).defaultPrevented’)
为什么?我做错了什么?
没有$超时在功能和测试它工作正常.
在此先感谢您的帮助.