Skip to content

Instantly share code, notes, and snippets.

@willm
Created July 25, 2013 15:33
Show Gist options
  • Save willm/6080936 to your computer and use it in GitHub Desktop.
Save willm/6080936 to your computer and use it in GitHub Desktop.
Spy function
var pSlice = Array.prototype.slice;
function createSpy(fn) {
function spy() {
// clone and coerce arguments into an array
var spiedArgs = pSlice.call(arguments);
// remember the arguments the spy was called with
spy.calls.push(spiedArgs);
// call the "behaviour" function passed when creating this
// spy with the actual arguments passed at runtime.
return fn && fn(spiedArgs);
}
// memo of calls made to the spy
spy.calls = [];
return spy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment