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

21,148

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

Share:
21,148
Shashank Vivek
Author by

Shashank Vivek

Give and you shall receive !

Updated on July 09, 2022

Comments

  • Shashank Vivek
    Shashank Vivek almost 2 years

    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