Skip to content

Commit

Permalink
Merge pull request sinonjs#1738 from nfriedly/issue-1736
Browse files Browse the repository at this point in the history
Fix onCall(#) with chained behaviors
  • Loading branch information
fatso83 authored Mar 28, 2018
2 parents f2696bd + 1414afb commit cee8156
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function callCallback(behavior, args) {

var proto = {
create: function create(stub) {
var behavior = extend({}, proto);
var behavior = extend(stub.bind(), proto);
delete behavior.create;
delete behavior.addBehavior;
delete behavior.createBehavior;
Expand Down Expand Up @@ -219,7 +219,7 @@ function createBehavior(behaviorMethod) {
function addBehavior(stub, name, fn) {
proto[name] = function () {
fn.apply(this, [this].concat([].slice.call(arguments)));
return this.stub || this;
return this;
};

stub[name] = createBehavior(name);
Expand Down
38 changes: 38 additions & 0 deletions test/issues/issues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,42 @@ describe("issues", function () {
refute.isTrue(spyProp.get.called);
});
});

describe("#1736 - onCall + yield + returns", function () {
it("should return the correct value without onCall", function () {
var stub = sinon.stub().yields("yield val").returns("return val");
var cb = sinon.spy();

var ret = stub(cb);

assert.equals(ret, "return val");
assert.equals(cb.args, [["yield val"]]);
});
it("should return the correct value with onCall", function () {
var stub = sinon.stub().onCall(0).yields("yield val").returns("return val");
var cb = sinon.spy();

var ret = stub(cb);

assert.equals(ret, "return val");
assert.equals(cb.args, [["yield val"]]);
});
it("should return the correct value with multiple onCalls", function () {
var stub = sinon.stub()
.onCall(0).yields("yield val").returns("return val")
.onCall(1).yields("second yield val").returns("second return val");
var cb = sinon.spy();
var cb2 = sinon.spy();

var ret = stub(cb);
var ret2 = stub(cb2);


assert.equals(ret, "return val");
assert.equals(cb.args, [["yield val"]]);

assert.equals(ret2, "second return val");
assert.equals(cb2.args, [["second yield val"]]);
});
});
});

0 comments on commit cee8156

Please sign in to comment.