Skip to content

Commit

Permalink
Allow stubbing getters and setters for function properties
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed Mar 11, 2017
1 parent 278c2ce commit 5bbfa51
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/sinon/stub.js
Expand Up @@ -41,14 +41,15 @@ function stub(object, property, descriptor) {
return stub.create();
}

var s = stub.create(arity);
s.rootObj = object;
s.propName = property;

if (isStubbingNonFuncProperty) {
var s = stub.create();
s.rootObj = object;
s.propName = property;
return s;
}

return wrapMethod(object, property, stub.create(arity));
return wrapMethod(object, property, s);
}

stub.createStubInstance = function (constructor) {
Expand Down
30 changes: 30 additions & 0 deletions test/stub-test.js
Expand Up @@ -2295,6 +2295,20 @@ describe("stub", function () {
assert.equals(myObj.prop, "bar");
});

it("allows users to stub getter functions for functions", function () {
var myObj = {
prop: function propGetter() {
return "foo";
}
};

createStub(myObj, "prop").get(function () {
return "bar";
});

assert.equals(myObj.prop, "bar");
});

it("replaces old getters", function () {
var myObj = {
get prop() {
Expand Down Expand Up @@ -2335,6 +2349,22 @@ describe("stub", function () {
assert.equals(myObj.example, "bar");
});

it("allows users to stub setter functions for functions", function () {
var myObj = {
prop: function propSetter() {
return "foo";
}
};

createStub(myObj, "prop").set(function () {
myObj.example = "bar";
});

myObj.prop = "baz";

assert.equals(myObj.example, "bar");
});

it("replaces old setters", function () {
var myObj = { // eslint-disable-line accessor-pairs
set prop(val) {
Expand Down

0 comments on commit 5bbfa51

Please sign in to comment.