Correctly teardown spies on inherited methods
- If the spied method is not an own property of the object being spied upon, the method is deleted from the object on teardown instead of being set to the original implementation. - #737
This commit is contained in:
@@ -33,25 +33,34 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
|
||||
throw new Error(methodName + ' is not declared writable or has no setter');
|
||||
}
|
||||
|
||||
var spy = j$.createSpy(methodName, obj[methodName]);
|
||||
var originalMethod = obj[methodName],
|
||||
spiedMethod = j$.createSpy(methodName, originalMethod),
|
||||
restoreStrategy;
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(obj, methodName)) {
|
||||
restoreStrategy = function() {
|
||||
obj[methodName] = originalMethod;
|
||||
};
|
||||
} else {
|
||||
restoreStrategy = function() {
|
||||
delete obj[methodName];
|
||||
};
|
||||
}
|
||||
|
||||
currentSpies().push({
|
||||
spy: spy,
|
||||
baseObj: obj,
|
||||
methodName: methodName,
|
||||
originalValue: obj[methodName]
|
||||
restoreObjectToOriginalState: restoreStrategy
|
||||
});
|
||||
|
||||
obj[methodName] = spy;
|
||||
obj[methodName] = spiedMethod;
|
||||
|
||||
return spy;
|
||||
return spiedMethod;
|
||||
};
|
||||
|
||||
this.clearSpies = function() {
|
||||
var spies = currentSpies();
|
||||
for (var i = 0; i < spies.length; i++) {
|
||||
var spyEntry = spies[i];
|
||||
spyEntry.baseObj[spyEntry.methodName] = spyEntry.originalValue;
|
||||
spyEntry.restoreObjectToOriginalState();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user