Created
October 2, 2015 12:14
-
-
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
This file contains hidden or 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
| 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