Testing how many times a method was called from another method's return statement in AngularJS

59 views Asked by At

Newbie to JS. I have this function. First, it checks for data in cache. If it's not there, it calls backend for this data and saves it to cache.

function doSomething() {

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

I need to test how many times cache and backend are called on the first and the second time the method doSomething is executed. Here is my test:

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

However, I am getting an error saying that call count for getCachedData and getData is always 0, both the first and the second time.

Changing this.$rootScope.$apply(); to this.$rootScope.$digest(); doesn't improve anything. I also tested it manually and everything seems to be working fine.

I also noticed that if I change the function doSomething as below then the counts for getCachedData are 2 & 2, which is correct.

function doSomething() {

    this.getCachedData();

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

There are 1 answers

2
Timetrax On BEST ANSWER

create an object. for example:

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++;}
}

in

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

in

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

I need to test how many times cache and backend are called on the first and the second time the method doSomething is executed

in

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
  }
}

at anytime during execution.. lets say at the end of the day you can now check system_metrics.cache_calls to give you total number of cache calls for the day.. thats if you never resetted the cache calls

at anytime during execution you can clear the number of cache calls with system_metrics.reset_cache_calls

if your question is to check how many times cache was called when doSomething runs for the first time or second time.

if your question is to check how many times backend was called when doSomething runs for the first time or second time.

add the following to your system_metrics do_something_calls add_do_something_call reset_do_something_call & add add_do_something_call to your do_something function i think you get the picture

with this approach you can track any metric that you want anytime that you want to