【问题标题】:Testing how many times a method was called from another method's return statement in AngularJS测试从AngularJS中另一个方法的return语句调用一个方法的次数
【发布时间】:2020-01-08 17:38:52
【问题描述】:

JS 新手。我有这个功能。首先,它检查缓存中的数据。如果不存在,它会为此数据调用后端并将其保存到缓存中。

function doSomething() {

    return getCachedData()
        .catch(function() {
            return getData()
              .then(saveDataToCache);
        });
}

我需要测试在第一次和第二次执行方法 doSomething 时调用缓存和后端的次数。这是我的测试:

it('should test', function() {
        spyOn(this.service, 'doSomething').andCallThrough();
        spyOn(this.service, 'getCachedData').andCallThrough();
        spyOn(this.service, 'getData').andCallThrough();

        this.service.doSomething();
        this.$rootScope.$apply();

        expect(this.service.doSomething.callCount).toBe(1);
        expect(this.service.getCachedData.callCount).toBe(1);
        expect(this.service.getData.callCount).toBe(1);

        this.service.doSomething();
        this.$rootScope.$apply();

        expect(this.service.doSomething.callCount).toBe(2);
        expect(this.service.getCachedData.callCount).toBe(2);
        expect(this.service.getData.callCount).toBe(1);
    });

但是,我收到一条错误消息,指出 getCachedData 和 getData 的调用计数始终为 0,无论是第一次还是第二次。

this.$rootScope.$apply(); 更改为 this.$rootScope.$digest(); 并没有改善任何东西。我还手动对其进行了测试,一切似乎都运行良好。

我还注意到,如果我将函数 doSomething 更改如下,那么 getCachedData 的计数是 2 和 2,这是正确的。

function doSomething() {

    this.getCachedData();

    return getCachedData()
        .catch(function() {
            return getData()
              .then(saveDataToCache);
        });
}

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine


    【解决方案1】:

    创建一个对象。例如:

    system_metrics ={
     cache_calls:0,
     backend_calls:0,
     reset_cache:function(){this.cache_calls=0;},
     reset_backend_calls:function(){this.backend_calls=0;},
     add_cache_call:function(){this.cache_calls++;}
     add_backend_call:function(){this.cache_calls++;}
    }
    

    function getCachedData(){
        //add this line
       system_metrics.add_cache_call();
    }
    

    function getData(){
        //add this line
       system_metrics.add_backend_call();
    }
    

    我需要测试在第一次和第二次执行方法 doSomething 时调用缓存和后端的次数

    function doSomething(){
      if(system_metrics.cache_calls === 1){
         //the first time the method doSomething is executed
      }
      if(system_metrics.cache_calls === 2){
         //the second time the method doSomething is executed
      }
      if(system_metrics.backend_calls === 1){
         //the first time the method doSomething is executed
      }
      if(system_metrics.backend_calls === 2){
         //the second time the method doSomething is executed
      }
    }
    

    在执行期间的任何时候.. 让我们说在一天结束时 您现在可以查看system_metrics.cache_calls 以获取当天的缓存调用总数。如果您从未重置缓存调用

    在执行期间的任何时候,您都可以使用system_metrics.reset_cache_calls清除缓存调用次数

    如果您的问题是检查缓存被调用了多少次 当 doSomething 第一次或第二次运行时。

    如果您的问题是检查后端被调用了多少次 当 doSomething 第一次或第二次运行时。

    将以下内容添加到您的 system_metrics do_something_calls add_do_something_call reset_do_something_call 并将add_do_something_call 添加到您的do_something function 我想你明白了

    通过这种方法,您可以随时跟踪所需的任何指标

    【讨论】:

    • 谢谢。好东西。我认为这比我需要的要多得多。换句话说,我需要编写一个单元测试来确认每次调用 doSomething() 时都会调用 getCachedData()。并且只有在没有缓存数据的情况下才会调用 getData。
    • 该对象包含测试所需的所有数据。测试变得非常容易编写..所以这是您的程序的测试驱动方法。这使得测试非常简单>>> 例如:: 在你的单元测试中.. when hasCachedData() && doSomething() // assertThat ( system_metrics.cachecalls ==1 && system_metrics.backend_calls =0) when clearCache() && doSomething () // assertThat (system_metrics.cache_calls ===1 && system_metrics.backend_calls === 1)
    猜你喜欢
    • 1970-01-01
    • 2020-05-28
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多