Skip to content

Commit 5bbfa51

Browse files
committed
Allow stubbing getters and setters for function properties
1 parent 278c2ce commit 5bbfa51

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

lib/sinon/stub.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ function stub(object, property, descriptor) {
4141
return stub.create();
4242
}
4343

44+
var s = stub.create(arity);
45+
s.rootObj = object;
46+
s.propName = property;
47+
4448
if (isStubbingNonFuncProperty) {
45-
var s = stub.create();
46-
s.rootObj = object;
47-
s.propName = property;
4849
return s;
4950
}
5051

51-
return wrapMethod(object, property, stub.create(arity));
52+
return wrapMethod(object, property, s);
5253
}
5354

5455
stub.createStubInstance = function (constructor) {

test/stub-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,20 @@ describe("stub", function () {
22952295
assert.equals(myObj.prop, "bar");
22962296
});
22972297

2298+
it("allows users to stub getter functions for functions", function () {
2299+
var myObj = {
2300+
prop: function propGetter() {
2301+
return "foo";
2302+
}
2303+
};
2304+
2305+
createStub(myObj, "prop").get(function () {
2306+
return "bar";
2307+
});
2308+
2309+
assert.equals(myObj.prop, "bar");
2310+
});
2311+
22982312
it("replaces old getters", function () {
22992313
var myObj = {
23002314
get prop() {
@@ -2335,6 +2349,22 @@ describe("stub", function () {
23352349
assert.equals(myObj.example, "bar");
23362350
});
23372351

2352+
it("allows users to stub setter functions for functions", function () {
2353+
var myObj = {
2354+
prop: function propSetter() {
2355+
return "foo";
2356+
}
2357+
};
2358+
2359+
createStub(myObj, "prop").set(function () {
2360+
myObj.example = "bar";
2361+
});
2362+
2363+
myObj.prop = "baz";
2364+
2365+
assert.equals(myObj.example, "bar");
2366+
});
2367+
23382368
it("replaces old setters", function () {
23392369
var myObj = { // eslint-disable-line accessor-pairs
23402370
set prop(val) {

0 commit comments

Comments
 (0)