Skip to content

Commit

Permalink
Restore sinon.createStubInstance() behaviour (#2073)
Browse files Browse the repository at this point in the history
PR #2022 redirected sinon.createStubInstance() to use the Sandbox
implementation thereof. This introduced a breaking change due to the
sandbox implementation not supporting property overrides.

Instead of duplicating the original behaviour from stub.js into
sandbox.js, call through to the stub.js implementation then add all
the stubs to the sandbox collection as usual.

Copy the missing tests from stub-test.js and add issue tests.
  • Loading branch information
kevinbosman authored and mroderick committed Sep 2, 2019
1 parent 766cae4 commit 157b537
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/sinon/sandbox.js
Expand Up @@ -51,11 +51,18 @@ function Sandbox() {
return fakeRestorers;
};

sandbox.createStubInstance = function createStubInstance(constructor) {
if (typeof constructor !== "function") {
throw new TypeError("The constructor should be a function.");
}
return this.stub(Object.create(constructor.prototype));
sandbox.createStubInstance = function createStubInstance() {
var stubbed = sinonStub.createStubInstance.apply(null, arguments);

var ownMethods = collectOwnMethods(stubbed);

forEach(ownMethods, function(method) {
push(collection, method);
});

usePromiseLibrary(promiseLib, ownMethods);

return stubbed;
};

sandbox.inject = function inject(obj) {
Expand Down
23 changes: 23 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -657,4 +657,27 @@ describe("issues", function() {
});
});
});

describe("#2073 - sinon.createStubInstance()", function() {
function Foo() {
return;
}
Foo.prototype.testMethod = function() {
return 1;
};

it("should override the method", function() {
var thing = sinon.createStubInstance(Foo, {
testMethod: sinon.stub().returns(2)
});
assert.equals(thing.testMethod(), 2);
});

it("should support calling without object binding", function() {
var createStubInstance = sinon.createStubInstance;
refute.exception(function() {
createStubInstance(Foo);
});
});
});
});
65 changes: 65 additions & 0 deletions test/sandbox-test.js
Expand Up @@ -280,6 +280,71 @@ describe("Sandbox", function() {
});
}
});

it("allows providing optional overrides", function() {
var Class = function() {
return;
};
Class.prototype.method = function() {
return;
};

var stub = this.sandbox.createStubInstance(Class, {
method: sinonStub().returns(3)
});

assert.equals(3, stub.method());
});

it("allows providing optional returned values", function() {
var Class = function() {
return;
};
Class.prototype.method = function() {
return;
};

var stub = this.sandbox.createStubInstance(Class, {
method: 3
});

assert.equals(3, stub.method());
});

it("allows providing null as a return value", function() {
var Class = function() {
return;
};
Class.prototype.method = function() {
return;
};

var stub = this.sandbox.createStubInstance(Class, {
method: null
});

assert.equals(null, stub.method());
});

it("throws an exception when trying to override non-existing property", function() {
var Class = function() {
return;
};
Class.prototype.method = function() {
return;
};

var sandbox = this.sandbox;

assert.exception(
function() {
sandbox.createStubInstance(Class, {
foo: sinonStub().returns(3)
});
},
{ message: "Cannot stub foo. Property does not exist!" }
);
});
});

describe(".stub", function() {
Expand Down

0 comments on commit 157b537

Please sign in to comment.