Skip to content

Commit

Permalink
Merge pull request #1516 from fatso83/1512-sandbox-stub-props-on-prot…
Browse files Browse the repository at this point in the history
…otype

Fix for 1512: sandbox.stub() fails on prototype props
  • Loading branch information
fatso83 authored Aug 7, 2017
2 parents ec74e94 + b4a3f42 commit 75ad693
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/sinon/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ var collection = {
},

stub: function stub(object, property) {
if (object && typeof property !== "undefined" && !Object.prototype.hasOwnProperty.call(object, property)) {
if (object && typeof property !== "undefined"
&& !(property in object)) {
throw new TypeError("Cannot stub non-existent own property " + valueToString(property));
}

Expand Down
21 changes: 21 additions & 0 deletions test/issues/issues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,25 @@ describe("issues", function () {
assert.equals(this.stub.withArgs("arg").lastCall.returnValue, "return value");
});
});

describe("#1512", function () {
var sandbox;

beforeEach(function () {
sandbox = sinon.sandbox.create();
});

afterEach(function () {
sandbox.restore();
});

it("can stub methods on the prototype", function () {
var proto = { someFunction: function () {} };
var instance = Object.create(proto);

var stub = sandbox.stub(instance, "someFunction");
instance.someFunction();
assert(stub.called);
});
});
});

0 comments on commit 75ad693

Please sign in to comment.