Skip to content

Instantly share code, notes, and snippets.

@spencercarnage
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spencercarnage/2c05a81f64a9401a3e48 to your computer and use it in GitHub Desktop.
Save spencercarnage/2c05a81f64a9401a3e48 to your computer and use it in GitHub Desktop.
Jasmine's `andReturn` vs `andCallFake`
// 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