From 73d2ac8e320682fee3041b3acfdf9b9db7b12bdd Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Tue, 12 Mar 2019 21:23:04 +0100 Subject: [PATCH 1/2] Remove unused prop 'injectIntoThis' Was not used anywhere in the code, except as setup in tests --- lib/sinon/util/core/default-config.js | 1 - test/sandbox-test.js | 7 ------- test/util/core/get-config-test.js | 2 -- 3 files changed, 10 deletions(-) diff --git a/lib/sinon/util/core/default-config.js b/lib/sinon/util/core/default-config.js index 765197eb1..44914c35d 100644 --- a/lib/sinon/util/core/default-config.js +++ b/lib/sinon/util/core/default-config.js @@ -1,7 +1,6 @@ "use strict"; module.exports = { - injectIntoThis: true, injectInto: null, properties: ["spy", "stub", "mock", "clock", "server", "requests"], useFakeTimers: true, diff --git a/test/sandbox-test.js b/test/sandbox-test.js index 5dd9c95b4..79ecdbc35 100644 --- a/test/sandbox-test.js +++ b/test/sandbox-test.js @@ -1671,7 +1671,6 @@ describe("Sandbox", function() { it("yields stub, mock as arguments", function() { var sandbox = createSandbox( sinonConfig({ - injectIntoThis: false, properties: ["stub", "mock"] }) ); @@ -1686,7 +1685,6 @@ describe("Sandbox", function() { it("yields spy, stub, mock as arguments", function() { var sandbox = createSandbox( sinonConfig({ - injectIntoThis: false, properties: ["spy", "stub", "mock"] }) ); @@ -1701,7 +1699,6 @@ describe("Sandbox", function() { it("does not yield server when not faking xhr", function() { var sandbox = createSandbox( sinonConfig({ - injectIntoThis: false, properties: ["server", "stub", "mock"], useFakeServer: false }) @@ -1740,7 +1737,6 @@ describe("Sandbox", function() { it("yields server when faking xhr", function() { var sandbox = createSandbox( sinonConfig({ - injectIntoThis: false, properties: ["server", "stub", "mock"] }) ); @@ -1756,7 +1752,6 @@ describe("Sandbox", function() { it("uses serverWithClock when faking xhr", function() { var sandbox = createSandbox( sinonConfig({ - injectIntoThis: false, properties: ["server"], useFakeServer: fakeServerWithClock }) @@ -1784,7 +1779,6 @@ describe("Sandbox", function() { it("yields clock when faking timers", function() { var sandbox = createSandbox( sinonConfig({ - injectIntoThis: false, properties: ["server", "clock"] }) ); @@ -1846,7 +1840,6 @@ describe("Sandbox", function() { it("fakes specified timers", function() { var sandbox = createSandbox( sinonConfig({ - injectIntoThis: false, properties: ["clock"], useFakeTimers: { toFake: ["Date", "setTimeout"] } }) diff --git a/test/util/core/get-config-test.js b/test/util/core/get-config-test.js index 7e7c26c79..32d2b9563 100644 --- a/test/util/core/get-config-test.js +++ b/test/util/core/get-config-test.js @@ -12,7 +12,6 @@ describe("core/util/getConfig", function() { var config = getConfig(); refute.same(config, defaultConfig); - assert.equals(config.injectIntoThis, defaultConfig.injectIntoThis); assert.equals(config.injectInto, defaultConfig.injectInto); assert.equals(config.properties, defaultConfig.properties); assert.equals(config.useFakeTimers, defaultConfig.useFakeTimers); @@ -26,7 +25,6 @@ describe("core/util/getConfig", function() { }); refute.same(config, defaultConfig); - assert.equals(config.injectIntoThis, defaultConfig.injectIntoThis); assert.equals(config.injectInto, defaultConfig.injectInto); assert.equals(config.properties, ["stub", "mock"]); assert.equals(config.useFakeTimers, defaultConfig.useFakeTimers); From 20eeb48c4d781260d5b6fdb8037cbc6239052a55 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Tue, 12 Mar 2019 21:57:44 +0100 Subject: [PATCH 2/2] Inject createStubInstance and fake functionality --- lib/sinon/sandbox.js | 20 +++++++++++++++++ lib/sinon/util/core/default-config.js | 14 +++++++++++- test/sandbox-test.js | 32 ++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/sinon/sandbox.js b/lib/sinon/sandbox.js index 38ea72820..8a9123b5e 100644 --- a/lib/sinon/sandbox.js +++ b/lib/sinon/sandbox.js @@ -71,6 +71,26 @@ function Sandbox() { return sandbox.mock.apply(null, arguments); }; + obj.createStubInstance = function() { + return sandbox.createStubInstance.apply(sandbox, arguments); + }; + + obj.fake = function() { + return sandbox.fake.apply(null, arguments); + }; + + obj.replace = function() { + return sandbox.replace.apply(null, arguments); + }; + + obj.replaceSetter = function() { + return sandbox.replaceSetter.apply(null, arguments); + }; + + obj.replaceGetter = function() { + return sandbox.replaceGetter.apply(null, arguments); + }; + if (sandbox.clock) { obj.clock = sandbox.clock; } diff --git a/lib/sinon/util/core/default-config.js b/lib/sinon/util/core/default-config.js index 44914c35d..d47ca9aae 100644 --- a/lib/sinon/util/core/default-config.js +++ b/lib/sinon/util/core/default-config.js @@ -2,7 +2,19 @@ module.exports = { injectInto: null, - properties: ["spy", "stub", "mock", "clock", "server", "requests"], + properties: [ + "spy", + "stub", + "mock", + "clock", + "server", + "requests", + "fake", + "replace", + "replaceSetter", + "replaceGetter", + "createStubInstance" + ], useFakeTimers: true, useFakeServer: true }; diff --git a/test/sandbox-test.js b/test/sandbox-test.js index 79ecdbc35..315e713f3 100644 --- a/test/sandbox-test.js +++ b/test/sandbox-test.js @@ -1551,12 +1551,42 @@ describe("Sandbox", function() { this.sandbox.restore(); }); - it("injects spy, stub, mock", function() { + it("injects spy, stub, mock, fake, replace, replaceSetter, createStubInstance", function() { this.sandbox.inject(this.obj); assert.isFunction(this.obj.spy); assert.isFunction(this.obj.stub); assert.isFunction(this.obj.mock); + assert.isFunction(this.obj.createStubInstance); + assert.isFunction(this.obj.fake); + assert.isFunction(this.obj.replace); + assert.isFunction(this.obj.replaceSetter); + assert.isFunction(this.obj.replaceGetter); + }); + + it("should inject callable functions", function() { + /* eslint-disable no-empty-function, accessor-pairs */ + this.sandbox.inject(this.obj); + + var myObj = { a: function() {} }; + function MyClass() {} + Object.defineProperty(myObj, "b", { + get: function() { + return 42; + }, + configurable: true + }); + Object.defineProperty(myObj, "c", { set: function() {}, configurable: true }); + + refute.exception( + function() { + this.obj.createStubInstance(MyClass); + var fake = this.obj.fake(); + this.obj.replace(myObj, "a", fake); + this.obj.replaceGetter(myObj, "b", fake); + this.obj.replaceSetter(myObj, "c", fake); + }.bind(this) + ); }); it("does not define clock, server and requests objects", function() {