Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always chain behavior with stub #1227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function throwsException(error, message) {
this.exception = error;
}

return this;
return this.chain();
}

function getCallback(behavior, args) {
Expand Down Expand Up @@ -182,7 +182,7 @@ var proto = {

callsFake: function callsFake(fn) {
this.fakeFn = fn;
return this;
return this.chain();
},

callsArg: function callsArg(pos) {
Expand All @@ -196,7 +196,7 @@ var proto = {
this.callArgProp = undefined;
this.callbackAsync = false;

return this;
return this.chain();
},

callsArgOn: function callsArgOn(pos, context) {
Expand All @@ -210,7 +210,7 @@ var proto = {
this.callArgProp = undefined;
this.callbackAsync = false;

return this;
return this.chain();
},

callsArgWith: function callsArgWith(pos) {
Expand All @@ -224,7 +224,7 @@ var proto = {
this.callArgProp = undefined;
this.callbackAsync = false;

return this;
return this.chain();
},

callsArgOnWith: function callsArgWith(pos, context) {
Expand All @@ -238,7 +238,7 @@ var proto = {
this.callArgProp = undefined;
this.callbackAsync = false;

return this;
return this.chain();
},

yields: function () {
Expand All @@ -248,7 +248,7 @@ var proto = {
this.callArgProp = undefined;
this.callbackAsync = false;

return this;
return this.chain();
},

yieldsRight: function () {
Expand All @@ -258,7 +258,7 @@ var proto = {
this.callArgProp = undefined;
this.callbackAsync = false;

return this;
return this.chain();
},

yieldsOn: function (context) {
Expand All @@ -268,7 +268,7 @@ var proto = {
this.callArgProp = undefined;
this.callbackAsync = false;

return this;
return this.chain();
},

yieldsTo: function (prop) {
Expand All @@ -278,7 +278,7 @@ var proto = {
this.callArgProp = prop;
this.callbackAsync = false;

return this;
return this.chain();
},

yieldsToOn: function (prop, context) {
Expand All @@ -288,7 +288,7 @@ var proto = {
this.callArgProp = prop;
this.callbackAsync = false;

return this;
return this.chain();
},

throws: throwsException,
Expand All @@ -302,7 +302,7 @@ var proto = {
this.exception = undefined;
this.fakeFn = undefined;

return this;
return this.chain();
},

returnsArg: function returnsArg(pos) {
Expand All @@ -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) {
Expand All @@ -329,7 +329,7 @@ var proto = {
this.exception = undefined;
this.fakeFn = undefined;

return this;
return this.chain();
},

rejects: function rejects(error, message) {
Expand All @@ -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;
}
};

Expand Down
11 changes: 11 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down