Testing a debounced function in AngularJS with Jasmine never calls the function

后端 未结 3 1565
耶瑟儿~
耶瑟儿~ 2021-02-20 03:57

I have a method in a service that uses underscore\'s debounce.

Inside that method is a call to a method on a different service. I\'m trying to test that the different s

3条回答
  •  Happy的楠姐
    2021-02-20 04:40

    Angular $timeout has advantage in tests because it is mocked in tests to be synchronous. The one won't have this advantage when third-party asynchronous tools one used. In general asynchronous specs will look like that:

    var maxDelay = 500;
    
      ...
      it('has a method getMind, that calls herp.aMethod', function (done){
        whoa.getMind();
        setTimeout(function () {
          expect(herp.aMethod).toHaveBeenCalled();
          done();
        }, maxDelay);
      });
    

    Since Underscore debounce doesn't offer flush functionality (while the recent version of Lodash debounce does), asynchronous testing is the best option available.

提交回复
热议问题