javascript – 角茉莉花:’undefined’不是一个对象 – 在超时内广播 – 错误

前端之家收集整理的这篇文章主要介绍了javascript – 角茉莉花:’undefined’不是一个对象 – 在超时内广播 – 错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的功能: @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’)

为什么?我做错了什么?
没有$超时在功能和测试它工作正常.

在此先感谢您的帮助.

猜你在找的JavaScript相关文章