August 2016
Intermediate to advanced
635 pages
14h 5m
English
A spy is an approach that wraps all the methods of an object and records the inputs and outputs from that method as well as the number of calls. By wrapping the calls, it is possible to examine exactly what was passed in and what came out of the function. Test spies can be used when the exact inputs into a function are not known beforehand.
In other languages, building test spies requires reflection and can be quite complicated. We can actually get away with making a basic test spy in no more than a couple of lines of code. Let's experiment.
To start we'll need a class to intercept:
var SpyUpon = (function () {
function SpyUpon() {
}
SpyUpon.prototype.write = function (toWrite) {
console.log(toWrite);
};
return SpyUpon;
})();Now we would ...
Read now
Unlock full access