I am struggling to comprehend the last test in the Jasmine 2.2 documentation which showcases the basic usage of Spies.
In the beforeEach()
section, we initialize bar = null
, then we spy on foo.setBar
and proceed to call foo.setBar
twice. I am puzzled as to why bar === null
in the final test. Shouldn't it be bar === 456
before the spy is removed for the test?
Here is the scenario:
describe("About a Spy", function(){
var foo, bar = null;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
};
spyOn(foo, "setBar"); // we spy
foo.setBar(123); // shouldn't bar === 123 here?
foo.setBar(456, 'another param'); // and bar === 456 here?
});
it("stops all execution on a function", function() {
// What, why, how?
expect(bar).toBeNull();
//I expected this to be the case, but it's not.
//expect(bar).toBe(456);
});
});
I seem to be misinterpreting how the beforeEach function sets up and clears the variable scope, or maybe there is a point where the variables within the describe
block get reset? Or maybe they were never actually altered because we only used the spy function and not the actual function?
It would be greatly appreciated if someone could clarify what exactly is happening with the variable bar
in this test suite so I can understand why its value remains null in the last test.
Thank you!