Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created February 28, 2012 22:16
Show Gist options
  • Save unscriptable/1935618 to your computer and use it in GitHub Desktop.
Save unscriptable/1935618 to your computer and use it in GitHub Desktop.
spy on a method
function MethodSpy (method, context) {
var count, orig, spy;
count = 0;
orig = context[method];
context[method] = function () {
count++;
return orig.apply(context, arguments);
};
return {
calledNever: function () { return count == 0; },
calledOnce: function () { return count == 1; },
calledTwice: function () { return count == 2; },
calledMany: function (howMany) { return count == howMany; },
unspy: function () { context[method] = orig; }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment