Skip to content

Instantly share code, notes, and snippets.

@sfoster
Created April 6, 2011 17:09
Show Gist options
  • Save sfoster/906064 to your computer and use it in GitHub Desktop.
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
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