Skip to content

Commit c89fb21

Browse files
committed
Deprecate stubbing sandbox value in favor of value behavior
1 parent dbefd45 commit c89fb21

File tree

5 files changed

+159
-7
lines changed

5 files changed

+159
-7
lines changed

lib/sinon/collection.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
var sinonSpy = require("./spy");
44
var sinonStub = require("./stub");
55
var sinonMock = require("./mock");
6-
var throwOnFalsyObject = require("./throw-on-falsy-object");
6+
var sandboxStub = require("./sandbox-stub");
77
var collectOwnMethods = require("./collect-own-methods");
8-
var stubNonFunctionProperty = require("./stub-non-function-property");
98

109
var push = [].push;
1110

@@ -81,13 +80,12 @@ var collection = {
8180
},
8281

8382
stub: function stub(object, property/*, value*/) {
84-
throwOnFalsyObject.apply(null, arguments);
83+
if (arguments.length > 2) {
84+
return sandboxStub.apply(this, arguments);
85+
}
8586

87+
var stubbed = sinonStub.apply(null, arguments);
8688
var isStubbingEntireObject = typeof property === "undefined" && typeof object === "object";
87-
var isStubbingNonFunctionProperty = property && typeof object[property] !== "function";
88-
var stubbed = isStubbingNonFunctionProperty ?
89-
stubNonFunctionProperty.apply(null, arguments) :
90-
sinonStub.apply(null, arguments);
9189

9290
if (isStubbingEntireObject) {
9391
var ownMethods = collectOwnMethods(stubbed);

lib/sinon/default-behaviors.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use strict";
2+
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
23

34
var slice = [].slice;
45
var useLeftMostCallback = -1;
@@ -186,6 +187,24 @@ module.exports = {
186187
set: setterFunction
187188
});
188189

190+
return fake;
191+
},
192+
193+
value: function value(fake, newVal) {
194+
var rootStub = fake.stub || fake;
195+
196+
var oldVal = getPropertyDescriptor(rootStub.rootObj, rootStub.propName).value;
197+
198+
Object.defineProperty(rootStub.rootObj, rootStub.propName, {
199+
value: newVal
200+
});
201+
202+
fake.restore = function restore() {
203+
Object.defineProperty(rootStub.rootObj, rootStub.propName, {
204+
value: oldVal
205+
});
206+
};
207+
189208
return fake;
190209
}
191210
};

lib/sinon/sandbox-stub.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
3+
var collectOwnMethods = require("./collect-own-methods");
4+
var deprecated = require("./util/core/deprecated");
5+
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
6+
var stubNonFunctionProperty = require("./stub-non-function-property");
7+
var sinonStub = require("./stub");
8+
var throwOnFalsyObject = require("./throw-on-falsy-object");
9+
10+
// This is deprecated and will be removed in a future version of sinon.
11+
// We will only consider pull requests that fix serious bugs in the implementation
12+
function sandboxStub(object, property/*, value*/) {
13+
deprecated.printWarning(
14+
"sandbox.stub(obj, 'meth', val) is deprecated and will be removed from " +
15+
"the public API in a future version of sinon." +
16+
"\n Use sandbox(obj, 'meth').callsFake(fn) instead in order to stub a function." +
17+
"\n Use sandbox(obj, 'meth').value(fn) instead in order to stub a non-function value."
18+
);
19+
20+
throwOnFalsyObject.apply(null, arguments);
21+
22+
var actualDescriptor = getPropertyDescriptor(object, property);
23+
var isStubbingEntireObject = typeof property === "undefined" && typeof object === "object";
24+
var isStubbingNonFuncProperty = typeof object === "object"
25+
&& typeof property !== "undefined"
26+
&& (typeof actualDescriptor === "undefined"
27+
|| typeof actualDescriptor.value !== "function");
28+
29+
30+
// When passing a value as third argument it will be applied to stubNonFunctionProperty
31+
var stubbed = isStubbingNonFuncProperty ?
32+
stubNonFunctionProperty.apply(null, arguments) :
33+
sinonStub.apply(null, arguments);
34+
35+
if (isStubbingEntireObject) {
36+
var ownMethods = collectOwnMethods(stubbed);
37+
ownMethods.forEach(this.add.bind(this));
38+
if (this.promiseLibrary) {
39+
ownMethods.forEach(this.addUsingPromise.bind(this));
40+
}
41+
} else {
42+
this.add(stubbed);
43+
if (this.promiseLibrary) {
44+
stubbed.usingPromise(this.promiseLibrary);
45+
}
46+
}
47+
48+
return stubbed;
49+
}
50+
51+
module.exports = sandboxStub;

test/sandbox-test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,66 @@ describe("sinonSandbox", function () {
548548
sandbox.restore();
549549
});
550550
});
551+
552+
describe("getters and setters", function () {
553+
it("allows stubbing getters", function () {
554+
var object = {
555+
foo: "bar"
556+
};
557+
558+
var sandbox = sinonSandbox.create();
559+
sandbox.stub(object, "foo").get(function () {
560+
return "baz";
561+
});
562+
563+
assert.equals(object.foo, "baz");
564+
});
565+
566+
it("allows restoring getters", function () {
567+
var object = {
568+
foo: "bar"
569+
};
570+
571+
var sandbox = sinonSandbox.create();
572+
sandbox.stub(object, "foo").get(function () {
573+
return "baz";
574+
});
575+
576+
sandbox.restore();
577+
578+
assert.equals(object.foo, "bar");
579+
});
580+
581+
it("allows stubbing setters", function () {
582+
var object = {
583+
prop: "bar"
584+
};
585+
586+
var sandbox = sinonSandbox.create();
587+
sandbox.stub(object, "foo").set(function (val) {
588+
object.prop = val + "bla";
589+
});
590+
591+
object.foo = "bla";
592+
593+
assert.equals(object.prop, "blabla");
594+
});
595+
596+
it("allows restoring setters", function () {
597+
var object = {
598+
prop: "bar"
599+
};
600+
601+
var sandbox = sinonSandbox.create();
602+
sandbox.stub(object, "prop").set(function setterFn(val) {
603+
object.prop = val + "bla";
604+
});
605+
606+
sandbox.restore();
607+
608+
object.prop = "bla";
609+
610+
assert.equals(object.prop, "bla");
611+
});
612+
});
551613
});

test/stub-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,4 +2524,26 @@ describe("stub", function () {
25242524
assert.equals(myObj.otherProp, "bar");
25252525
});
25262526
});
2527+
2528+
describe(".value", function () {
2529+
it("allows stubbing property descriptor values", function () {
2530+
var myObj = {
2531+
prop: "rawString"
2532+
};
2533+
2534+
createStub(myObj, "prop").value("newString");
2535+
assert.equals(myObj.prop, "newString");
2536+
});
2537+
2538+
it("allows restoring stubbed property descriptor values", function () {
2539+
var myObj = {
2540+
prop: "rawString"
2541+
};
2542+
2543+
var stub = createStub(myObj, "prop").value("newString");
2544+
stub.restore();
2545+
2546+
assert.equals(myObj.prop, "rawString");
2547+
});
2548+
});
25272549
});

0 commit comments

Comments
 (0)