From b723992f7aa4cc48b43f493352fa771f49956a58 Mon Sep 17 00:00:00 2001 From: Viktor Zozulyak Date: Sun, 1 Jan 2017 20:10:45 +0100 Subject: [PATCH] Always chain behavior with stub --- lib/sinon/behavior.js | 40 ++++++++++++++++++++++++---------------- test/stub-test.js | 11 +++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/sinon/behavior.js b/lib/sinon/behavior.js index fdba4e4b6..0bf20b1df 100644 --- a/lib/sinon/behavior.js +++ b/lib/sinon/behavior.js @@ -42,7 +42,7 @@ function throwsException(error, message) { this.exception = error; } - return this; + return this.chain(); } function getCallback(behavior, args) { @@ -182,7 +182,7 @@ var proto = { callsFake: function callsFake(fn) { this.fakeFn = fn; - return this; + return this.chain(); }, callsArg: function callsArg(pos) { @@ -196,7 +196,7 @@ var proto = { this.callArgProp = undefined; this.callbackAsync = false; - return this; + return this.chain(); }, callsArgOn: function callsArgOn(pos, context) { @@ -210,7 +210,7 @@ var proto = { this.callArgProp = undefined; this.callbackAsync = false; - return this; + return this.chain(); }, callsArgWith: function callsArgWith(pos) { @@ -224,7 +224,7 @@ var proto = { this.callArgProp = undefined; this.callbackAsync = false; - return this; + return this.chain(); }, callsArgOnWith: function callsArgWith(pos, context) { @@ -238,7 +238,7 @@ var proto = { this.callArgProp = undefined; this.callbackAsync = false; - return this; + return this.chain(); }, yields: function () { @@ -248,7 +248,7 @@ var proto = { this.callArgProp = undefined; this.callbackAsync = false; - return this; + return this.chain(); }, yieldsRight: function () { @@ -258,7 +258,7 @@ var proto = { this.callArgProp = undefined; this.callbackAsync = false; - return this; + return this.chain(); }, yieldsOn: function (context) { @@ -268,7 +268,7 @@ var proto = { this.callArgProp = undefined; this.callbackAsync = false; - return this; + return this.chain(); }, yieldsTo: function (prop) { @@ -278,7 +278,7 @@ var proto = { this.callArgProp = prop; this.callbackAsync = false; - return this; + return this.chain(); }, yieldsToOn: function (prop, context) { @@ -288,7 +288,7 @@ var proto = { this.callArgProp = prop; this.callbackAsync = false; - return this; + return this.chain(); }, throws: throwsException, @@ -302,7 +302,7 @@ var proto = { this.exception = undefined; this.fakeFn = undefined; - return this; + return this.chain(); }, returnsArg: function returnsArg(pos) { @@ -312,13 +312,13 @@ var proto = { this.returnArgAt = pos; - return this; + return this.chain(); }, returnsThis: function returnsThis() { this.returnThis = true; - return this; + return this.chain(); }, resolves: function resolves(value) { @@ -329,7 +329,7 @@ var proto = { this.exception = undefined; this.fakeFn = undefined; - return this; + return this.chain(); }, rejects: function rejects(error, message) { @@ -349,7 +349,15 @@ var proto = { this.exception = undefined; this.fakeFn = undefined; - return this; + return this.chain(); + }, + + chain: function chain() { + /** + * "this" is stub when method is called directly on stub, e.g. stub.returns(123); + * "this.stub" exists when method is called from onCall chaining, e.g. stub.onCall(0).returns(123) + */ + return this.stub || this; } }; diff --git a/test/stub-test.js b/test/stub-test.js index 6fb09d69f..c3efaffc8 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -1719,6 +1719,17 @@ describe("stub", function () { } }); + it("supports chained declaration of behavior", function () { + var stub = createStub() + .onCall(0).returns(1) + .onCall(1).returns(2) + .onCall(2).returns(3); + + assert.same(stub(), 1); + assert.same(stub(), 2); + assert.same(stub(), 3); + }); + describe("in combination with withArgs", function () { it("can produce a sequence for a fake", function () { var stub = createStub().returns(0);