Skip to content

Commit cee8156

Browse files
authored
Merge pull request #1738 from nfriedly/issue-1736
Fix onCall(#) with chained behaviors
2 parents f2696bd + 1414afb commit cee8156

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

lib/sinon/behavior.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function callCallback(behavior, args) {
101101

102102
var proto = {
103103
create: function create(stub) {
104-
var behavior = extend({}, proto);
104+
var behavior = extend(stub.bind(), proto);
105105
delete behavior.create;
106106
delete behavior.addBehavior;
107107
delete behavior.createBehavior;
@@ -219,7 +219,7 @@ function createBehavior(behaviorMethod) {
219219
function addBehavior(stub, name, fn) {
220220
proto[name] = function () {
221221
fn.apply(this, [this].concat([].slice.call(arguments)));
222-
return this.stub || this;
222+
return this;
223223
};
224224

225225
stub[name] = createBehavior(name);

test/issues/issues-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,42 @@ describe("issues", function () {
420420
refute.isTrue(spyProp.get.called);
421421
});
422422
});
423+
424+
describe("#1736 - onCall + yield + returns", function () {
425+
it("should return the correct value without onCall", function () {
426+
var stub = sinon.stub().yields("yield val").returns("return val");
427+
var cb = sinon.spy();
428+
429+
var ret = stub(cb);
430+
431+
assert.equals(ret, "return val");
432+
assert.equals(cb.args, [["yield val"]]);
433+
});
434+
it("should return the correct value with onCall", function () {
435+
var stub = sinon.stub().onCall(0).yields("yield val").returns("return val");
436+
var cb = sinon.spy();
437+
438+
var ret = stub(cb);
439+
440+
assert.equals(ret, "return val");
441+
assert.equals(cb.args, [["yield val"]]);
442+
});
443+
it("should return the correct value with multiple onCalls", function () {
444+
var stub = sinon.stub()
445+
.onCall(0).yields("yield val").returns("return val")
446+
.onCall(1).yields("second yield val").returns("second return val");
447+
var cb = sinon.spy();
448+
var cb2 = sinon.spy();
449+
450+
var ret = stub(cb);
451+
var ret2 = stub(cb2);
452+
453+
454+
assert.equals(ret, "return val");
455+
assert.equals(cb.args, [["yield val"]]);
456+
457+
assert.equals(ret2, "second return val");
458+
assert.equals(cb2.args, [["second yield val"]]);
459+
});
460+
});
423461
});

0 commit comments

Comments
 (0)