Skip to content

Instantly share code, notes, and snippets.

@zizon
Created August 5, 2013 07:04
Show Gist options
  • Save zizon/6153991 to your computer and use it in GitHub Desktop.
Save zizon/6153991 to your computer and use it in GitHub Desktop.
simple mocker
"use strict";
var mocker = function(self){
return {
"mocked":{},
"mock":function(name){
var current = this.mocked[name];
if(current != null) {
return current;
}
var root = this;
return this.mocked[name] = current = {
"origin":null,
"target":null,
"with":function(target){
this.origin = self[name];
self[name] = this.target = target;
return this;
},
"swap":function(name){
var current = root.mocked[name];
if( current != null ){
return this.with(this.origin);
}
return this;
},
"undo":function(){
self[name] = this.origin;
delete root.mocked[name];
return this;
},
"reset":function(){
var old = root.mocked;
root.mocked = {};
for(var item in old){
old[item].undo();
}
return this;
}
};
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment