From 8676bbf11ad35c168cf641eb2dfdb6caaad5161c Mon Sep 17 00:00:00 2001 From: Sean Parmelee Date: Thu, 1 Sep 2016 13:39:25 -0500 Subject: [PATCH] fallback on assignment when a spy cannot be deleted --- spec/core/SpyRegistrySpec.js | 25 ++++++++++++++++++++++++- src/core/SpyRegistry.js | 4 +++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/spec/core/SpyRegistrySpec.js b/spec/core/SpyRegistrySpec.js index dc1b1b8b..bbe45903 100644 --- a/spec/core/SpyRegistrySpec.js +++ b/spec/core/SpyRegistrySpec.js @@ -127,6 +127,29 @@ describe("SpyRegistry", function() { expect(subject.hasOwnProperty('spiedFunc')).toBe(false); expect(subject.spiedFunc).toBe(originalFunction); - }) + }); + + it("restores the original function when it\'s inherited and cannot be deleted", function() { + // IE 8 doesn't support `Object.create` or `Object.defineProperty` + if (jasmine.getEnv().ieVersion < 9) { return; } + + var spies = [], + spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}), + originalFunction = function() {}, + subjectParent = {spiedFunc: originalFunction}; + + var subject = Object.create(subjectParent); + + spyRegistry.spyOn(subject, 'spiedFunc'); + + // simulate a spy that cannot be deleted + Object.defineProperty(subject, 'spiedFunc', { + configurable: false + }); + + spyRegistry.clearSpies(); + + expect(jasmineUnderTest.isSpy(subject.spiedFunc)).toBe(false); + }); }); }); diff --git a/src/core/SpyRegistry.js b/src/core/SpyRegistry.js index 56ffb434..f53bf0ad 100644 --- a/src/core/SpyRegistry.js +++ b/src/core/SpyRegistry.js @@ -53,7 +53,9 @@ getJasmineRequireObj().SpyRegistry = function(j$) { }; } else { restoreStrategy = function() { - delete obj[methodName]; + if (!delete obj[methodName]) { + obj[methodName] = originalMethod; + } }; }