Add spyOnAllFunctions function

This function will spy on all writable and configurable functionss of
an object that is passed in. It can be used like this:

    spyOnAllFunctions(obj);

This commit addresses https://github.com/jasmine/jasmine/issues/1421
This commit is contained in:
Andrew Eisenberg
2018-07-18 20:04:31 -07:00
parent 110c092c9e
commit f62eb3b1a8
3 changed files with 120 additions and 0 deletions

View File

@@ -120,6 +120,23 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
return spy;
};
this.spyOnAllFunctions = function(obj) {
if (j$.util.isUndefined(obj)) {
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);
}
}
}
return obj;
};
this.clearSpies = function() {
var spies = currentSpies();
for (var i = spies.length - 1; i >= 0; i--) {

View File

@@ -235,6 +235,18 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.spyOnProperty(obj, methodName, accessType);
},
/**
* Installs spies on all writable and configurable properties of an object.
* @name spyOnProperty
* @function
* @global
* @param {Object} obj - The object upon which to install the {@link Spy}s
* @returns {Object} the spied object
*/
spyOnAllFunctions: function(obj) {
return env.spyOnAllFunctions(obj);
},
jsApiReporter: new jasmine.JsApiReporter({
timer: new jasmine.Timer()
}),