Created
August 5, 2013 07:04
-
-
Save zizon/6153991 to your computer and use it in GitHub Desktop.
simple mocker
This file contains 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
"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