I am learning to implement karma
& Jasmine
in AngularJS and I am going through some of its examples to understand it better.
I am little bit confused about callThrough
.
Please correct me if I am misunderstanding it, it's slightly similar to callFake()
in a way that we are not actually calling the function. In callFake()
we also provide a function with the return type but not in callThrough
.
From Jasmine doc:
By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation.
Please shed some light on this.
After struggling through it, i realized that an article is worth sharing. Here is a Medium article to anyone who stumbles across this question
callThrough()Tell the spy to call through to the real implementation when invoked.
Call-through telecom providers use Voice over IP (VoIP) call termination to connect the caller to the recipient. Most of the call is carried over IP network, where cost of carrying of the audio signal is cheaper than traditional phone carriers.
spyOn() takes two parameters: the first parameter is the name of the object and the second parameter is the name of the method to be spied upon. It replaces the spied method with a stub, and does not actually execute the real method. The spyOn() function can however be called only on existing methods.
Your understanding looks good:
Spies: and.callThrough
By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation.
Spies: and.callFake
By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied function.
If the function being spied on receives arguments that the fake needs, you can get those as well
Plus: when you add callThrough. you are not only tracking the calls by the spy. You also call the method to test it. The method here is actually called. While on callFake you only test if its gets called correctly by checking the arguments. The real method is not called. That makes sence since its called fake call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With