Skip to content

Commit

Permalink
Add .resolves and .rejects to stub (#1211)
Browse files Browse the repository at this point in the history
* Add .resolves and .rejects to stub

This commit adds the possibility for a stub
to be configured to return a promise which will
either resolves or reject

This was already possible, but this adds a shorthand for it

stub.returns(Promise.resolve({})) -> stub.resolve({})
stub.returns(Promise.reject(new Error("bad luck"))) -> stub.rejects("bad luck")

Since Promise are now part of the standard ES6 features, this
commit does not make use of polyfill or third party library.
This is problematic for some browsers and generally for contexts where
the standard is not implemented.

* Use native-promise-only for .resolves and .rejects

* change test titles for chainable methods
  • Loading branch information
Floby authored and fatso83 committed Dec 23, 2016
1 parent b5d10a4 commit 2df140f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var extend = require("./extend");
var functionName = require("./util/core/function-name");
var valueToString = require("./util/core/value-to-string");
var Promise = require("native-promise-only");

var slice = Array.prototype.slice;
var join = Array.prototype.join;
Expand Down Expand Up @@ -312,6 +313,33 @@ var proto = {
returnsThis: function returnsThis() {
this.returnThis = true;

return this;
},

resolves: function resolves(value) {
this.returnValue = Promise.resolve(value);
this.returnValueDefined = true;
this.exception = undefined;
this.fakeFn = undefined;

return this;
},

rejects: function rejects(error, message) {
var reason;
if (typeof error === "string") {
reason = new Error(message || "");
reason.name = error;
} else if (!error) {
reason = new Error("Error");
} else {
reason = error;
}
this.returnValue = Promise.reject(reason);
this.returnValueDefined = true;
this.exception = undefined;
this.fakeFn = undefined;

return this;
}
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"diff": "^3.1.0",
"formatio": "1.1.1",
"lolex": "^1.4.0",
"native-promise-only": "^0.8.1",
"path-to-regexp": "^1.7.0",
"samsam": "^1.1.3",
"text-encoding": "0.5.2"
Expand Down
88 changes: 88 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,94 @@ describe("stub", function () {
});
});

describe(".resolves", function () {
it("returns a promise to the specified value", function () {
var stub = createStub.create();
var object = {};
stub.resolves(object);

return stub().then(function (actual) {
assert.same(actual, object);
});
});

it("should return the same stub", function () {
var stub = createStub.create();

assert.same(stub.resolves(""), stub);
});

it("supersedes previous throws", function () {
var stub = createStub.create();
stub.throws().resolves(1);

refute.exception(function () {
stub();
});
});

it("supersedes previous rejects", function () {
var stub = createStub.create();
stub.rejects(Error("should be superseeded")).resolves(1);

return stub().then();
});
});

describe(".rejects", function () {
it("returns a promise which rejects for the specified reason", function () {
var stub = createStub.create();
var reason = new Error();
stub.rejects(reason);

return stub().then(function () {
referee.fail("this should not resolve");
}).catch(function (actual) {
assert.same(actual, reason);
});
});

it("should return the same stub", function () {
var stub = createStub.create();

assert.same(stub.rejects({}), stub);
});

it("specifies exception message", function () {
var stub = createStub.create();
var message = "Oh no!";
stub.rejects("Error", message);

return stub().then(function () {
referee.fail("Expected stub to reject");
}).catch(function (reason) {
assert.equals(reason.message, message);
});
});

it("does not specify exception message if not provided", function () {
var stub = createStub.create();
stub.rejects("Error");

return stub().then(function () {
referee.fail("Expected stub to reject");
}).catch(function (reason) {
assert.equals(reason.message, "");
});
});

it("rejects for a generic reason", function () {
var stub = createStub.create();
stub.rejects();

return stub().then(function () {
referee.fail("Expected stub to reject");
}).catch(function (reason) {
assert.equals(reason.name, "Error");
});
});
});

describe(".returnsArg", function () {
it("returns argument at specified index", function () {
var stub = createStub.create();
Expand Down

0 comments on commit 2df140f

Please sign in to comment.