Skip to content

Commit

Permalink
Remove deprecated
Browse files Browse the repository at this point in the history
* sandbox.stub(obj, 'meth', val)
* sinon.stub(obj, 'meth', fn)
* sinon/util/core
  • Loading branch information
mroderick committed Jul 26, 2017
1 parent 80946c9 commit 74263dd
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 115 deletions.
8 changes: 5 additions & 3 deletions docs/release-source/release/stubs.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ Replaces `object.method` with a stub function. An exception is thrown if the pro

The original function can be restored by calling `object.method.restore();` (or `stub.restore();`).

#### `var stub = sinon.stub(object, "method", func);`
#### ~~`var stub = sinon.stub(object, "method", func);`~~

Replaces `object.method` with a `func`, wrapped in a `spy`.
This has been removed from `v3.0.0`. Instead you should use

As usual, `object.method.restore();` can be used to restore the original method.
`stub(obj, 'meth').callsFake(fn)`

Codemod available at https://github.com/hurrymaplelad/sinon-codemod

#### `var stub = sinon.stub(obj);`

Expand Down
10 changes: 7 additions & 3 deletions lib/sinon/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
var sinonSpy = require("./spy");
var sinonStub = require("./stub");
var sinonMock = require("./mock");
var sandboxStub = require("./sandbox-stub");
var collectOwnMethods = require("./collect-own-methods");
var valueToString = require("./util/core/value-to-string");

var push = [].push;

Expand Down Expand Up @@ -85,9 +85,13 @@ var collection = {
return this.add(sinonSpy.apply(sinonSpy, arguments));
},

stub: function stub(object, property/*, value*/) {
stub: function stub(object, property) {
if (arguments.length > 2) {
return sandboxStub.apply(this, arguments);
throw new TypeError("stub(obj, 'meth', fn) has been removed, see documentation");
}

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

This comment has been minimized.

Copy link
@fatso83

fatso83 Aug 18, 2017

Contributor

@mroderick: do you remember why this guard clause was added? It removed the previously supported behaviour, see #1537.

}

var stubbed = sinonStub.apply(null, arguments);
Expand Down
17 changes: 4 additions & 13 deletions lib/sinon/sandbox-stub.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
"use strict";

var collectOwnMethods = require("./collect-own-methods");
var deprecated = require("./util/core/deprecated");
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
var stubNonFunctionProperty = require("./stub-non-function-property");
var sinonStub = require("./stub");
var throwOnFalsyObject = require("./throw-on-falsy-object");

// This is deprecated and will be removed in a future version of sinon.
// We will only consider pull requests that fix serious bugs in the implementation
function sandboxStub(object, property/*, value*/) {
deprecated.printWarning(
"sandbox.stub(obj, 'meth', val) is deprecated and will be removed from " +
"the public API in a future version of sinon." +
"\n Use sandbox.stub(obj, 'meth').callsFake(fn) instead in order to stub a function." +
"\n Use sandbox.stub(obj, 'meth').value(fn) instead in order to stub a non-function value."
);
function sandboxStub(object, property) {
if (arguments.length > 2) {
throw new TypeError("stub(obj, 'meth', fn) has been removed, see documentation");
}

throwOnFalsyObject.apply(null, arguments);

Expand All @@ -25,9 +19,6 @@ function sandboxStub(object, property/*, value*/) {
&& typeof property !== "undefined"
&& (typeof actualDescriptor === "undefined"
|| typeof actualDescriptor.value !== "function");


// When passing a value as third argument it will be applied to stubNonFunctionProperty
var stubbed = isStubbingNonFuncProperty ?
stubNonFunctionProperty.apply(null, arguments) :
sinonStub.apply(null, arguments);
Expand Down
41 changes: 0 additions & 41 deletions lib/sinon/stub-descriptor.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/sinon/stub-non-function-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
var valueToString = require("./util/core/value-to-string");
var hasOwnProperty = Object.prototype.hasOwnProperty;

function stubNonFunctionProperty(object, property, value) {
function stubNonFunctionProperty(object, property) {
var original = object[property];

if (!hasOwnProperty.call(object, property)) {
throw new TypeError("Cannot stub non-existent own property " + valueToString(property));
}

object[property] = value;

return {
restore: function restore() {
object[property] = original;
Expand Down
15 changes: 6 additions & 9 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@ var functionToString = require("./util/core/function-to-string");
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
var wrapMethod = require("./util/core/wrap-method");
var stubEntireObject = require("./stub-entire-object");
var stubDescriptor = require("./stub-descriptor");
var throwOnFalsyObject = require("./throw-on-falsy-object");

var slice = Array.prototype.slice;

function stub(object, property, descriptor) {
function stub(object, property) {
if (arguments.length > 2) {
throw new TypeError("stub(obj, 'meth', fn) has been removed, see documentation");
}

throwOnFalsyObject.apply(null, arguments);

var actualDescriptor = getPropertyDescriptor(object, property);
var isStubbingEntireObject = typeof property === "undefined" && typeof object === "object";
var isCreatingNewStub = !object && typeof property === "undefined";
var isStubbingDescriptor = object && property && Boolean(descriptor);
var isStubbingNonFuncProperty = (typeof object === "object" || typeof object === "function")
&& typeof property !== "undefined"
&& (typeof actualDescriptor === "undefined"
|| typeof actualDescriptor.value !== "function")
&& typeof descriptor === "undefined";
var isStubbingExistingMethod = !isStubbingDescriptor
&& typeof object === "object"
var isStubbingExistingMethod = typeof object === "object"
&& typeof actualDescriptor !== "undefined"
&& typeof actualDescriptor.value === "function";
var arity = isStubbingExistingMethod ? object[property].length : 0;
Expand All @@ -35,10 +36,6 @@ function stub(object, property, descriptor) {
return stubEntireObject(stub, object);
}

if (isStubbingDescriptor) {
return stubDescriptor.apply(null, arguments);
}

if (isCreatingNewStub) {
return stub.create();
}
Expand Down
12 changes: 5 additions & 7 deletions test/collection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe("collection", function () {
var originalPrintWarning = deprecated.printWarning;
deprecated.printWarning = function () {};

this.collection.stub(process.env, "HELL", "froze over");
this.collection.stub(process.env, "HELL").value("froze over");
assert.equals(process.env.HELL, "froze over");

deprecated.printWarning = originalPrintWarning;
Expand All @@ -132,7 +132,7 @@ describe("collection", function () {
var originalPrintWarning = deprecated.printWarning;
deprecated.printWarning = function () {};

this.collection.stub(this.object, "property", 1);
this.collection.stub(this.object, "property").value(1);

assert.equals(this.object.property, 1);

Expand All @@ -143,7 +143,7 @@ describe("collection", function () {
var originalPrintWarning = deprecated.printWarning;
deprecated.printWarning = function () {};

this.collection.stub(this.object, "property", 1);
this.collection.stub(this.object, "property").value(1);
this.collection.restore();

assert.equals(this.object.property, 42);
Expand Down Expand Up @@ -174,10 +174,8 @@ describe("collection", function () {
deprecated.printWarning = function () {};

assert.exception(function () {
collection.stub(object, Symbol(), 1);
}, function (err) {
return err.message === "Cannot stub non-existent own property Symbol()";
});
collection.stub(object, Symbol());
}, {message: "Cannot stub non-existent own property Symbol()"}, TypeError);

deprecated.printWarning = originalPrintWarning;
}
Expand Down
1 change: 1 addition & 0 deletions test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ describe("sinonSandbox", function () {

it("allows stubbing setters", function () {
var object = {
foo: undefined,
prop: "bar"
};

Expand Down
41 changes: 5 additions & 36 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ describe("stub", function () {
);
});

it("fails if called with an empty property descriptor", function () {
var propertyKey = "ea762c6d-16ab-4ded-8bc2-3bc6f2de2925";
var object = {};

object[propertyKey] = "257b38d8-3c02-4353-82ab-b1b588be6990";

assert.exception(
function () {
createStub(object, propertyKey, {});
},
{
message: "Expected property descriptor to have at least one key"
}
);
});

it("throws a readable error if stubbing Symbol on null", function () {
if (typeof Symbol === "function") {
assert.exception(
Expand Down Expand Up @@ -856,27 +840,12 @@ describe("stub", function () {
}
});

it("warns provided function as stub, recommending callsFake instead", function () {
deprecated.printWarning.restore();

var called = false;
var infoStub = createStub(console, "info");
var stub = createStub(this.object, "method", function () {
called = true;
});

stub();

assert(called);
assert(infoStub.called);
});

it("throws if third argument is provided but not a proprety descriptor", function () {
it("throws when third argument is provided", function () {
var object = this.object;

assert.exception(function () {
createStub(object, "method", 1);
}, "TypeError");
}, {message: "stub(obj, 'meth', fn) has been removed, see documentation"}, "TypeError");
});

it("stubbed method should be proper stub", function () {
Expand Down Expand Up @@ -1027,10 +996,10 @@ describe("stub", function () {
fail("should not call getter");
}
};
var stub = createStub(obj, "prop", {get: function () {
return 43;
}});

var stub = createStub(obj, "prop").get(function () {
return 43;
});
assert.equals(obj.prop, 43);

stub.restore();
Expand Down

0 comments on commit 74263dd

Please sign in to comment.