Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need some practical example of "callThrough" & "callFake()" in Jasmine

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.

Update:

After struggling through it, i realized that an article is worth sharing. Here is a Medium article to anyone who stumbles across this question

like image 562
Shashank Vivek Avatar asked Jan 01 '17 10:01

Shashank Vivek


People also ask

What is the use of callThrough in Jasmine?

callThrough()Tell the spy to call through to the real implementation when invoked.

What is call through?

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.

How do you pass parameters in spyOn?

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.


1 Answers

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

like image 181
hasan Avatar answered Sep 22 '22 13:09

hasan