Skip to content

Commit

Permalink
Use last matching withArgs declaration when using matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy Shimizu committed Nov 10, 2016
1 parent e6d2bd2 commit 5c79afd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ function matchingFake(fakes, args, strict) {
return undefined;
}

for (var i = 0, l = fakes.length; i < l; i++) {
if (fakes[i].matches(args, strict)) {
return fakes[i];
var fakesIndex = fakes.length;

while (fakesIndex--) {
if (fakes[fakesIndex].matches(args, strict)) {
return fakes[fakesIndex];
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,24 @@ describe("stub", function () {
assert.equals(stub(), 23);
assert.equals(stub({ foo: "bar", bar: "foo" }), 99);
});

it("ensure stub uses last matching arguments", function () {
var stub = createStub().returns(23);
stub.withArgs(2).returns(9);
stub.withArgs(2).returns(99);

assert.equals(stub(), 23);
assert.equals(stub(2), 99);
});

it("ensure stub uses last matching sinonMatch arguments", function () {
var stub = createStub().returns(23);
stub.withArgs(2).returns(9);
stub.withArgs(sinonMatch(2)).returns(99);

assert.equals(stub(), 23);
assert.equals(stub(2), 99);
});
});

describe(".callsArgAsync", function () {
Expand Down

0 comments on commit 5c79afd

Please sign in to comment.