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

后端 未结 3 1564
耶瑟儿~
耶瑟儿~ 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条回答
  •  春和景丽
    2021-02-20 04:27

    You just need to mock lodash debounce method:

    describe('Whoa', function(){
      var $injector, whoa, herp;
    
      beforeEach(function(){
        module('derp');
        spyOn(_, 'debounce').and.callFake(function(cb) { return function() { cb(); } });
        inject(function(_$injector_){
          var Whoa;
          $injector = _$injector_;
          Whoa = $injector.get('Whoa');
          herp = $injector.get('herp');
          whoa = new Whoa();
        });
      });
    
      beforeEach(function(){
        spyOn(herp, 'aMethod').andCallThrough();
      });
    
      it('has a method getMind, that calls herp.aMethod', function(){
        whoa.getMind();
        expect(herp.aMethod).toHaveBeenCalled();
      });
    });
    

提交回复
热议问题