Skip to content

Commit

Permalink
add throwsArg(index) to stubs
Browse files Browse the repository at this point in the history
This was requested through sinonjs#1270
  • Loading branch information
seppevs committed Mar 8, 2017
1 parent e0cfccc commit 32757dc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/_releases/v1.17.6/stubs.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ Causes the stub to throw an exception of the provided type.

Causes the stub to throw the provided exception object.

#### `stub.throwsArg(index);`

Causes the stub to throw the argument exception at the provided index.

`stub.returnsArg(0);` causes the stub to throw the first argument exception.


#### `stub.callsArg(index);`

Expand Down
3 changes: 3 additions & 0 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ var proto = {
this.exception ||
typeof this.returnArgAt === "number" ||
this.returnThis ||
typeof this.throwArgAt === "number" ||
this.fakeFn ||
this.returnValueDefined);
},
Expand All @@ -126,6 +127,8 @@ var proto = {
return args[this.returnArgAt];
} else if (this.returnThis) {
return context;
} else if (typeof this.throwArgAt === "number") {
throw args[this.throwArgAt];
} else if (this.fakeFn) {
return this.fakeFn.apply(context, args);
} else if (this.resolve) {
Expand Down
8 changes: 8 additions & 0 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ module.exports = {
fake.returnArgAt = pos;
},

throwsArg: function throwsArg(fake, pos) {
if (typeof pos !== "number") {
throw new TypeError("argument index is not number");
}

fake.throwArgAt = pos;
},

returnsThis: function returnsThis(fake) {
fake.returnThis = true;
},
Expand Down
1 change: 1 addition & 0 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var proto = {

delete this.returnValue;
delete this.returnArgAt;
delete this.throwArgAt;
delete this.fakeFn;
this.returnThis = false;

Expand Down
28 changes: 28 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,34 @@ describe("stub", function () {
});
});

describe(".throwsArg", function () {
it("throws argument at specified index", function () {
var stub = createStub.create();
stub.throwsArg(0);
var expectedError = new Error("The expected error message");

assert.exception(function () {
stub(expectedError);
}, function (err) {
return err.message === expectedError.message;
});
});

it("returns stub", function () {
var stub = createStub.create();

assert.same(stub.throwsArg(0), stub);
});

it("throws TypeError if no index is specified", function () {
var stub = createStub.create();

assert.exception(function () {
stub.throwsArg();
}, "TypeError");
});
});

describe(".returnsThis", function () {
it("stub returns this", function () {
var instance = {};
Expand Down

0 comments on commit 32757dc

Please sign in to comment.