Skip to content

Commit

Permalink
Merge pull request #1183 from jishi/master
Browse files Browse the repository at this point in the history
Use last defined withArgs match when multiple conditions are met
  • Loading branch information
mroderick authored Nov 10, 2016
2 parents 35fe7d8 + aa6f3b6 commit f9350d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
10 changes: 4 additions & 6 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +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 matchingFakes = fakes.filter(function (fake) {
return fake.matches(args, strict);
});

return undefined;
return matchingFakes.pop();
}

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

it("ensure stub uses last matching arguments", function () {
var unmatchedValue = "d3ada6a0-8dac-4136-956d-033b5f23eadf";
var firstMatchedValue = "68128619-a639-4b32-a4a0-6519165bf301";
var secondMatchedValue = "4ac2dc8f-3f3f-4648-9838-a2825fd94c9a";
var expectedArgument = "3e1ed1ec-c377-4432-a33e-3c937f1406d1";

var stub = createStub().returns(unmatchedValue);

stub.withArgs(expectedArgument).returns(firstMatchedValue);
stub.withArgs(expectedArgument).returns(secondMatchedValue);

assert.equals(stub(), unmatchedValue);
assert.equals(stub(expectedArgument), secondMatchedValue);
});

it("ensure stub uses last matching sinonMatch arguments", function () {
var unmatchedValue = "0aa66a7d-3c50-49ef-8365-bdcab637b2dd";
var firstMatchedValue = "1ab2c601-7602-4658-9377-3346f6814caa";
var secondMatchedValue = "e2e31518-c4c4-4012-a61f-31942f603ffa";
var expectedArgument = "90dc4a22-ef53-4c62-8e05-4cf4b4bf42fa";

var stub = createStub().returns(unmatchedValue);
stub.withArgs(expectedArgument).returns(firstMatchedValue);
stub.withArgs(sinonMatch(expectedArgument)).returns(secondMatchedValue);

assert.equals(stub(), unmatchedValue);
assert.equals(stub(expectedArgument), secondMatchedValue);
});
});

describe(".callsArgAsync", function () {
Expand Down
8 changes: 4 additions & 4 deletions test/util/core/called-in-order-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe("util/core/calledInOrder", function () {
it("returns true, if stubs were called in given order", function () {
assert(calledInOrder([testObject1.someFunction, testObject2.otherFunction]));
assert(calledInOrder([testObject1.someFunction, testObject2.otherFunction,
testObject3.thirdFunction]));
testObject3.thirdFunction]));
});

it("returns false, if stubs were called in wrong order", function () {
assert( !calledInOrder([testObject2.otherFunction, testObject1.someFunction]));
assert( !calledInOrder([testObject2.otherFunction, testObject1.someFunction,
testObject3.thirdFunction]));
testObject3.thirdFunction]));
});
});

Expand All @@ -48,13 +48,13 @@ describe("util/core/calledInOrder", function () {
it("returns true, if stubs were called in given order", function () {
assert(calledInOrder(testObject1.someFunction, testObject2.otherFunction));
assert(calledInOrder(testObject1.someFunction, testObject2.otherFunction,
testObject3.thirdFunction));
testObject3.thirdFunction));
});

it("returns false, if stubs were called in wrong order", function () {
assert( !calledInOrder(testObject2.otherFunction, testObject1.someFunction));
assert( !calledInOrder(testObject2.otherFunction, testObject1.someFunction,
testObject3.thirdFunction));
testObject3.thirdFunction));
});
});
});

0 comments on commit f9350d8

Please sign in to comment.