我有这样的功能:
$scope.doIt = function( x, y )
{
$timeout( function ()
{
$rootScope.$broadcast( 'xxx',
{
message: xxx,
status: xxx
} );
} );
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,此功能正常.但在写测试时我遇到了一些麻烦.
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( …Run Code Online (Sandbox Code Playgroud)