Created
April 6, 2011 17:09
-
-
Save sfoster/906064 to your computer and use it in GitHub Desktop.
Simple mechanism for temporarily replacing a method's implementation e.g. for mocking during tests
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
var mockMethod = function(obj, methName, fn) { | |
var orig = obj[methName]; | |
var handle = [obj, methName, orig]; | |
obj[methName] = fn; | |
return handle; | |
}, unMockMethod = function(handle) { | |
handle[0][handle[1]] = handle[2]; | |
}; | |
// usage: | |
var hdl = mockMethod(dojo, "xhrGet", function() { | |
console.log("yep dojo.xhrGet would have been called"); | |
}); | |
dojo.xhrGet({ }); // logs, doesn't get anything | |
// restore it | |
unMockMethod(hdl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment