Skip to content

Commit

Permalink
Add support for passing a function to stub.throws(...).
Browse files Browse the repository at this point in the history
  • Loading branch information
George Schneeloch committed Aug 3, 2017
1 parent 5daa737 commit 56ca44f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/release-source/release/stubs.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ Causes the stub to throw an exception of the provided type.
Causes the stub to throw the provided exception object.


#### `stub.throws(function() { return new Error(); });`

Causes the stub to throw the function returned by the function.


#### `stub.rejects();`

Causes the stub to return a Promise which rejects with an exception (`Error`).
Expand Down
4 changes: 4 additions & 0 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ var proto = {

if (this.exception) {
throw this.exception;
} else if (this.exceptionCreator) {
this.exception = this.exceptionCreator();
this.exceptionCreator = undefined;
throw this.exception;
} else if (typeof this.returnArgAt === "number") {
return args[this.returnArgAt];
} else if (this.returnThis) {
Expand Down
18 changes: 14 additions & 4 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ var useLeftMostCallback = -1;
var useRightMostCallback = -2;

function throwsException(fake, error, message) {
if (typeof error === "string") {
fake.exception = new Error(message || "");
fake.exception.name = error;
if (typeof error === "function") {
fake.exceptionCreator = error;
} else if (typeof error === "string") {
fake.exceptionCreator = function () {
var newException = new Error(message || "");
newException.name = error;
return newException;
};
} else if (!error) {
fake.exception = new Error("Error");
fake.exceptionCreator = function () {
return new Error("Error");
};
} else {
fake.exception = error;
}
Expand Down Expand Up @@ -129,6 +136,7 @@ module.exports = {
fake.reject = false;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

Expand Down Expand Up @@ -158,6 +166,7 @@ module.exports = {
fake.reject = false;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

Expand All @@ -176,6 +185,7 @@ module.exports = {
fake.reject = true;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;

return fake;
Expand Down
27 changes: 27 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,22 @@ describe("stub", function () {

refute.defined(stub.invoking);
});

it("throws an exception created using a function", function () {
var stub = createStub.create();

function namedFunc() {
return new Error("not implemented");
}

stub.throws(namedFunc);

assert.exception(stub, {
message: "not implemented"
});
assert.same(stub.firstCall.exception.message, "not implemented");
assert.contains(stub.firstCall.toString(), "not implemented");
});
});

describe(".callsArg", function () {
Expand Down Expand Up @@ -872,6 +888,17 @@ describe("stub", function () {
assert(stub.threw("TypeError"));
});

it("handles threw properly for lazily instantiated Errors", function () {
var stub = createStub(this.object, "method");
stub.throws(function () {
return new TypeError();
});

assert.exception(this.object.method);

assert(stub.threw("TypeError"));
});

it("returns standalone stub without arguments", function () {
var stub = createStub();

Expand Down

0 comments on commit 56ca44f

Please sign in to comment.