Created
July 25, 2013 15:33
-
-
Save willm/6080936 to your computer and use it in GitHub Desktop.
Spy function
This file contains 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 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