Extend spyOnAllFunctions to include prototype and parent methods

This commit is contained in:
Elliot Nelson
2019-05-11 18:20:13 -04:00
parent 9a18fdca27
commit ee15309a8f
2 changed files with 56 additions and 8 deletions

View File

@@ -125,13 +125,25 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
throw new Error('spyOnAllFunctions could not find an object to spy upon');
}
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop] instanceof Function) {
var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
if ((descriptor.writable || descriptor.set) && descriptor.configurable) {
this.spyOn(obj, prop);
var pointer = obj,
props = [],
prop,
descriptor;
while (pointer) {
for (prop in pointer) {
if (Object.prototype.hasOwnProperty.call(pointer, prop) && pointer[prop] instanceof Function) {
descriptor = Object.getOwnPropertyDescriptor(pointer, prop);
if ((descriptor.writable || descriptor.set) && descriptor.configurable) {
props.push(prop);
}
}
}
pointer = Object.getPrototypeOf(pointer);
}
for (var i = 0; i < props.length; i++) {
this.spyOn(obj, props[i]);
}
return obj;