Reputation: 4237
I looked at the other questions regarding spying on functions in Jasmine but I didn't get my doubt answered there. I intend to use andCallThrough
to track my original function in my src
script. This is what I have:
describe("My Test to spy :", function() {
var mySpy = jasmine.createSpy(window, "login");
beforeEach(function(){
mySpy();
});
it("Expects login() will be called", function(){
expect(mySpy).toHaveBeenCalled();
});
});
So this test passes because its the spy that is being called right? Not the original implementation of the function. So if I use mySpy.andCallThrough()
it gives an error. The docs are all about chaining objects and for methods. Nothing for functions. Need some help.
Upvotes: 2
Views: 2862
Reputation: 110892
The problem is that you use createSpy
instead of spyOn
. createSpy
will create a new spy so you can't use andCallThrough
on it as there is no function to call. Using spyOn
will replace an existing function with the spy and save the old function in the spy. So when you use andCallThrough
it will call this old method.
You can use createSpy
but then you have to pass a name and the original function:
jasmine.createSpy('someName', window.login)
When you use `spyOn', you have to pass the an object holding the function and the name of the function:
jasmine.spyOn(window, 'login')
Upvotes: 4