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

Fix stubbing function objects #1968

Merged
merged 1 commit into from
Jan 19, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ function stub(object, property) {
}

var actualDescriptor = getPropertyDescriptor(object, property);
var isStubbingEntireObject = typeof property === "undefined" && typeof object === "object";
var isObject = typeof object === "object" || typeof object === "function";
var isStubbingEntireObject = typeof property === "undefined" && isObject;
var isCreatingNewStub = !object && typeof property === "undefined";
var isStubbingNonFuncProperty =
(typeof object === "object" || typeof object === "function") &&
isObject &&
typeof property !== "undefined" &&
(typeof actualDescriptor === "undefined" || typeof actualDescriptor.value !== "function") &&
typeof descriptor === "undefined";
Expand Down
32 changes: 28 additions & 4 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1267,11 +1267,9 @@ describe("stub", function() {
assert.equals(obj[0](), "stubbed value");
});

it("does not stub function object", function() {
it("does not stub string", function() {
assert.exception(function() {
createStub(function() {
return;
});
createStub("test");
});
});
});
Expand Down Expand Up @@ -1317,6 +1315,32 @@ describe("stub", function() {
assert.same(createStub(object), object);
});

it("returns function", function() {
var func = function() {
return;
};

assert.same(createStub(func), func);
});

it("stubs methods of function", function() {
var func = function() {
return;
};
func.func1 = function() {
return;
};
// eslint-disable-next-line no-proto
func.__proto__.func2 = function() {
return;
};

createStub(func);

assert.isFunction(func.func1.restore);
assert.isFunction(func.func2.restore);
});

it("only stubs functions", function() {
var object = { foo: "bar" };
createStub(object);
Expand Down