diff --git a/lib/sinon/util/core/called-in-order.js b/lib/sinon/util/core/called-in-order.js index 397f906ca..862684391 100644 --- a/lib/sinon/util/core/called-in-order.js +++ b/lib/sinon/util/core/called-in-order.js @@ -1,15 +1,34 @@ "use strict"; +var every = Array.prototype.every; + module.exports = function calledInOrder(spies) { + var callMap = {}; + + function hasCallsLeft(spy) { + if (callMap[spy.id] === undefined) { + callMap[spy.id] = 0; + } + + return callMap[spy.id] < spy.callCount; + } + if (arguments.length > 1) { spies = arguments; } - for (var i = 1, l = spies.length; i < l; i++) { - if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) { - return false; + return every.call(spies, function checkAdjacentCalls(spy, i) { + var calledBeforeNext = true; + + if (i !== spies.length - 1) { + calledBeforeNext = spy.calledBefore(spies[i + 1]); + } + + if (hasCallsLeft(spy) && calledBeforeNext) { + callMap[spy.id] += 1; + return true; } - } - return true; + return false; + }); }; diff --git a/test/issues/issues-test.js b/test/issues/issues-test.js index d973a303a..6f451b4dc 100644 --- a/test/issues/issues-test.js +++ b/test/issues/issues-test.js @@ -186,4 +186,19 @@ describe("issues", function () { assert(firstFake !== secondFake); }); }); + + describe("#1398", function () { + it("Call order takes into account both calledBefore and callCount", function () { + var s1 = sinon.spy(); + var s2 = sinon.spy(); + + s1(); + s2(); + s1(); + + assert.exception(function () { + sinon.assert.callOrder(s2, s1, s2); + }); + }); + }); });