Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore sinon.createStubInstance() behaviour #2075

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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