property-replacer
v0.0.2
Published
Safely replace a property of an object with the ability to restore to the original state. Also provide a way to snoop on function calls. Useful for test driven development.
Downloads
6
Maintainers
Readme
property-replacer
Write javascript unit tests where you can safely replace object properties and undo the replacement on teardown. Inspired by perl Test::Resub and python redef
Installation
npm install property-replacer
Usage
Replacer.replace(object, key, anything); --> replace any key on an object with anything. Replacer.reset(); --> put all replaced properties back to normal.
var Replacer = require('property-replacer');
// some constructor
var Cat = function() {
};
Cat.prototype.meow = function() { return 'meow'; };
Replacer.replace(Cat.prototype, 'meow', function() { return 'bark!'; });
var cat = new Cat();
var meow = cat.meow();
// returns bark!
Replacer.reset();
meow = cat.meow();
// returns meow