Here is how to route your spy to actually callThrough the second/subsequent times after it has been used to fake a method earlier.
I feel this illustrates what you've been trying to achieve. Fiddle to see it in action
var util = {
foo: function() {
console.log("Foo has been called")
}
}
someFunction = function() {
console.log("lets call utils foo");
util.foo();
console.log("lets call utils foo one more time");
util.foo();
console.log("lets call utils foo one last time");
util.foo();
}
describe("spec to util's foo multiple times", function() {
it('test', function() {
var self = this;
self.resetSpy = function() {
self.spyVar.and.callThrough();
};
self.spyVar = spyOn(util, 'foo').and.callFake(function(e) {
console.log("Foo hasn't been called, instead a Fake method has been called")
self.resetSpy();
});
someFunction();
expect(util.foo).toHaveBeenCalled();
});
});
Notes:
someFunction
is a function that calls util.foo
three times.
- Say you'd want to fake it once but the next two calls you want to
actually call the function itself, then we can initially set the spy
to use a fake method however for the second and third calls, we reset
teh spy to actually
callThrough
self.resetSpy
is an internal function on the spec that I invoke after the first spy call.
- more info on reset the spy is here
Hope this helps.