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);
});
}
create an object. for example:
in
in
in
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 callsat 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
& addadd_do_something_call
to yourdo_something function
i think you get the picturewith this approach you can track any metric that you want anytime that you want to