Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1994 from fatso83/expose-props-inject
Inject fake functionality and createStubInstance
  • Loading branch information
fatso83 committed Mar 20, 2019
2 parents 9faf58e + 20eeb48 commit 2119f08
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
20 changes: 20 additions & 0 deletions lib/sinon/sandbox.js
Expand Up @@ -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;
}
Expand Down
15 changes: 13 additions & 2 deletions lib/sinon/util/core/default-config.js
@@ -1,9 +1,20 @@
"use strict";

module.exports = {
injectIntoThis: true,
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
};
39 changes: 31 additions & 8 deletions test/sandbox-test.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -1671,7 +1701,6 @@ describe("Sandbox", function() {
it("yields stub, mock as arguments", function() {
var sandbox = createSandbox(
sinonConfig({
injectIntoThis: false,
properties: ["stub", "mock"]
})
);
Expand All @@ -1686,7 +1715,6 @@ describe("Sandbox", function() {
it("yields spy, stub, mock as arguments", function() {
var sandbox = createSandbox(
sinonConfig({
injectIntoThis: false,
properties: ["spy", "stub", "mock"]
})
);
Expand All @@ -1701,7 +1729,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
})
Expand Down Expand Up @@ -1740,7 +1767,6 @@ describe("Sandbox", function() {
it("yields server when faking xhr", function() {
var sandbox = createSandbox(
sinonConfig({
injectIntoThis: false,
properties: ["server", "stub", "mock"]
})
);
Expand All @@ -1756,7 +1782,6 @@ describe("Sandbox", function() {
it("uses serverWithClock when faking xhr", function() {
var sandbox = createSandbox(
sinonConfig({
injectIntoThis: false,
properties: ["server"],
useFakeServer: fakeServerWithClock
})
Expand Down Expand Up @@ -1784,7 +1809,6 @@ describe("Sandbox", function() {
it("yields clock when faking timers", function() {
var sandbox = createSandbox(
sinonConfig({
injectIntoThis: false,
properties: ["server", "clock"]
})
);
Expand Down Expand Up @@ -1846,7 +1870,6 @@ describe("Sandbox", function() {
it("fakes specified timers", function() {
var sandbox = createSandbox(
sinonConfig({
injectIntoThis: false,
properties: ["clock"],
useFakeTimers: { toFake: ["Date", "setTimeout"] }
})
Expand Down
2 changes: 0 additions & 2 deletions test/util/core/get-config-test.js
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2119f08

Please sign in to comment.