Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Inject createStubInstance and fake functionality
  • Loading branch information
fatso83 committed Mar 12, 2019
1 parent 73d2ac8 commit 20eeb48
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 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
14 changes: 13 additions & 1 deletion lib/sinon/util/core/default-config.js
Expand Up @@ -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
};
32 changes: 31 additions & 1 deletion 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

0 comments on commit 20eeb48

Please sign in to comment.