Last active
August 29, 2015 14:08
-
-
Save spencercarnage/2c05a81f64a9401a3e48 to your computer and use it in GitHub Desktop.
Jasmine's `andReturn` vs `andCallFake`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Jasmine's `andReturn` sets the value at instantiation and it can | |
// never be changed after that apparently. Use `andCallFake` instead | |
// if you need it to be dynamic. | |
var value = 9; | |
var getValueOne = jasmine.createSpy('spyOneAndReturn').andReturn(value); | |
var getValueTwo = jasmine.createSpy('spyOneCallFake').andCallFake(function () { | |
return value; | |
}); | |
var setValue = jasmine.createSpy('spyTwo').andCallFake(function (newValue) { | |
value = newValue; | |
}); | |
getValueOne(); // 9 | |
getValueTwo(); // 9 | |
setValue(10); | |
getValueOne(); // still returns 9 | |
getValueTwo(); // returns 10 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment