Last active
October 29, 2015 14:47
-
-
Save soarez/5136664a86f2ba49ff25 to your computer and use it in GitHub Desktop.
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
var util = require('util'); | |
var EventEmitter = require('events').EventEmitter; | |
function Spy(f, ctx) { | |
var ee = new EventEmitter(); | |
spy.called = false; | |
Object.keys(EventEmitter.prototype).forEach(function(p) { | |
spy[p] = transfer; | |
function transfer() { | |
var args = Array.prototype.slice.call(arguments); | |
return ee[p].apply(ee, args); | |
} | |
}); | |
return spy; | |
function spy() { | |
var args = Array.prototype.slice.call(arguments); | |
spy.called = true; | |
spy.emit('called', args); | |
var ret = f.apply(ctx || null, args); | |
spy.emit('returned', args, ret); | |
return ret; | |
} | |
} | |
// Usage | |
function theCallback() { | |
} | |
var spy = Spy(theCallback); | |
spy.on('called', function() { | |
console.log('Gotcha!', spy.called); | |
}); | |
spy(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment