TypeError: Cannot read property 'defaultPrevented' of undefined

前端 未结 1 1824
花落未央
花落未央 2021-02-05 19:56

I am getting this error when I do the following test:

it(\'should call pauseAnimationInterval if in focus\', inject(function(SearchBoxData, intervalManager, $ti         


        
1条回答
  •  臣服心动
    2021-02-05 20:05

    Scope events which are $broadcast()'d or $emit()'d are synchronous, and return the event object when they're finished.

    Some things in Angular depend on the returned event object, such as ngRoute.

    So what you can do to fix this is simply add andCallThrough() when you create your spy, so that it calls the original function and yields a return value.

    Alternatively, if you don't want to use the real $broadcast, you can use andCallFake() to supply a fake function which can return anything you want.

    Example:

    beforeEach(function(){
      module('MyApp');
      inject(function($injector){
        rootScope = $injector.get('$rootScope');
        spyOn(rootScope,'$broadcast').andCallThrough(); // will prevent the reference error
      })
    });
    

    0 讨论(0)
提交回复
热议问题