AngularJS $timeout function not executing in my Jasmine specs

前端 未结 3 1531
醉话见心
醉话见心 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:44

    As noted in one of the comments, Jasmine setTimeout mock is not being used because angular's JS mock $timeout service is used instead. Personally, I'd rather use Jasmine's because its mocking method lets me test the length of the timeout. You can effectively circumvent it with a simple provider in your unit test:

    module(function($provide) {
      $provide.constant('$timeout', setTimeout);
    });
    

    Note: if you go this route, be sure to call $scope.apply() after jasmine.Clock.tick.

提交回复
热议问题