Hi,
I'm starting with Jasmine and I quite don't understand the use of callFake.
In the official Jasmine docs this is what appears:
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
spyOn(foo, "getBar").and.callFake(function(arguments, can, be, received) {
return 1001;
});
foo.setBar(123);
fetchedBar = foo.getBar();
Where foo is the object and getBar the method being spied. So far so good. Then a function is passed as the argument in order to call the function in the callFake parameter instead of the actual getBar method. The callFake function will return 1001 when called, no matter what. Then we can read this:
it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(1001);
});
What I can't understand is: In which scenario I can expect the function
to not return 1001?. getbar is being replaced by the function passed in callFake, which will return 1001 every time it's called in that specific spec.
For what I can see, this is equivalent to this:
var a;
a = function(word){
console.log(word);// logs the word into the console
};
a("Hi didly ho!!");//Hi didly ho!!
a = function(word){
alert(word);
}
a("shut up flanders");// alert
a("okily dokily!!")//alert
The first time the function will log the string, the second and third time it will create an alert. I can't expect the function to log the string in the console any more.
I'm sorry if the question seems dumb, but I can get my head around it. Hopefully someone could point out a more realistic scenario that makes it more clear.
Thanks,
Rodrigo.