使用Jasmine在AngularJS中测试debounced函数从不调用该函数 [英] Testing a debounced function in AngularJS with Jasmine never calls the function

查看:109
本文介绍了使用Jasmine在AngularJS中测试debounced函数从不调用该函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用下划线去抖的服务中有一个方法。

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 service is called.

在我尝试测试debounced方法时,从不调用不同服务的方法,并且jasmine失败了:

In my attempts to test the debounced method, the different services' method is never called, and jasmine fails with:

预期间谍aMethod被调用。

"Expected spy aMethod to have been called."

我知道它被调用的事实(它在chrome中记录到控制台),它只是在期望已经失败后被调用。

I know for a fact that it IS called (it logs to console in chrome), it's just called AFTER the expectation already failed.

所以......(最好)不添加Sinon或其他依赖项,并且使用

奖励积分*给予解决方案不必转_.debounce进入$ timeout ...

So... (preferably) without adding Sinon or another dependency and with
bonus points* given to a solution doesn't have to turn the _.debounce into a $timeout...

怎么办?

angular.module('derp', [])
.service('herp', function(){ 
   return {
     aMethod: function(){ 
       console.log('called!'); 
       return 'blown'; 
     }
   }; 
 })
 .service('Whoa', ['herp', function(herp){
   function Whoa(){
     var that = this;
     this.mindStatus = 'meh';
     this.getMind = _.debounce(function(){
       that.mindStatus = herp.aMethod();
     }, 300);
   }
   return Whoa;
 }]);



测试:



tests:

describe('Whoa', function(){
  var $injector, whoa, herp;

  beforeEach(function(){
    module('derp');
    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();
  });
});

为什么AngularJS测试神会离弃我?

Why have the AngularJS Testing gods forsaken me?

*我不知道如何在stackoverflow上给出实际的奖励积分,但如果有可能,我会。

* I do not know how to give actual bonus points on stackoverflow, but if it is possible, i will.

推荐答案

你只需要模拟lodash debounce方法:

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();
  });
});

这篇关于使用Jasmine在AngularJS中测试debounced函数从不调用该函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆