From 32757dc4459487bbe57dda6d42106746716e4f07 Mon Sep 17 00:00:00 2001 From: Sebastian Van Sande Date: Wed, 8 Mar 2017 12:38:12 +0100 Subject: [PATCH] add throwsArg(index) to stubs This was requested through https://github.com/sinonjs/sinon/issues/1270 --- docs/_releases/v1.17.6/stubs.md | 6 ++++++ lib/sinon/behavior.js | 3 +++ lib/sinon/default-behaviors.js | 8 ++++++++ lib/sinon/stub.js | 1 + test/stub-test.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+) diff --git a/docs/_releases/v1.17.6/stubs.md b/docs/_releases/v1.17.6/stubs.md index 020f9ba42..7649e7275 100644 --- a/docs/_releases/v1.17.6/stubs.md +++ b/docs/_releases/v1.17.6/stubs.md @@ -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);` diff --git a/lib/sinon/behavior.js b/lib/sinon/behavior.js index 42112d778..f2daa0c77 100644 --- a/lib/sinon/behavior.js +++ b/lib/sinon/behavior.js @@ -113,6 +113,7 @@ var proto = { this.exception || typeof this.returnArgAt === "number" || this.returnThis || + typeof this.throwArgAt === "number" || this.fakeFn || this.returnValueDefined); }, @@ -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) { diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 263c500ca..3c285a57b 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -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; }, diff --git a/lib/sinon/stub.js b/lib/sinon/stub.js index 7580dda9f..5359015d3 100644 --- a/lib/sinon/stub.js +++ b/lib/sinon/stub.js @@ -92,6 +92,7 @@ var proto = { delete this.returnValue; delete this.returnArgAt; + delete this.throwArgAt; delete this.fakeFn; this.returnThis = false; diff --git a/test/stub-test.js b/test/stub-test.js index 8660c9f5a..d16dbd9b1 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -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 = {};