diff --git a/lib/sinon/sandbox.js b/lib/sinon/sandbox.js index 6bab45f96..e6d8a0274 100644 --- a/lib/sinon/sandbox.js +++ b/lib/sinon/sandbox.js @@ -243,7 +243,7 @@ function Sandbox(opts = {}) { */ function getFakeRestorer(object, property, forceAssignment = false) { const descriptor = getPropertyDescriptor(object, property); - const value = object[property]; + const value = forceAssignment && object[property]; function restorer() { if (forceAssignment) { diff --git a/test/sandbox-test.js b/test/sandbox-test.js index cc1818015..c3c507359 100644 --- a/test/sandbox-test.js +++ b/test/sandbox-test.js @@ -1296,6 +1296,20 @@ describe("Sandbox", function () { }, ); }); + + it("do not call getter whilst replacing getter", function () { + const sandbox = this.sandbox; + const fake = sandbox.fake.returns("bar"); + const object = { + get foo() { + return fake(); + }, + }; + + sandbox.replaceGetter(object, "foo", () => "replacement"); + + refute(fake.called); + }); }); describe(".replaceSetter", function () {