Skip to content

Instantly share code, notes, and snippets.

@gabssnake
Created October 2, 2015 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabssnake/f23a7a81143a993fa349 to your computer and use it in GitHub Desktop.
Save gabssnake/f23a7a81143a993fa349 to your computer and use it in GitHub Desktop.
Javascript Spy on object method. Keeps the arguments of each call and can be later destroyed
function Spy (object, method) {
var original = object[method];
var spy = {
calls: [],
destroy: function () {
object[method] = original;
}
};
object[method] = function () {
var args = Array.prototype.slice.call(arguments);
spy.calls.push(args);
original.apply(object, args);
};
object[method].prototype = original.prototype;
return spy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment