AngularJS $timeout function not executing in my Jasmine specs

前端 未结 3 1532
醉话见心
醉话见心 2021-02-06 21:42

I\'m trying to test my AngularJS controller with Jasmine, using Karma. But a $timeout which works well in real-life, crashes my tests.

Controller:



        
3条回答
  •  佛祖请我去吃肉
    2021-02-06 22:42

    As $timeout is just a wrapper for window.setTimeout, you can use jasmines Clock.useMock() which mocks the window.setTimeout

      beforeEach(function() {
        jasmine.Clock.useMock();
      });
    
      it('should do stuff', function() {
        $scope.doStuff();
        jasmine.Clock.tick(251);
        expect($scope.stuffDone).toBeTruthy();
      });
    

提交回复
热议问题