diff --git a/lib/sinon/spy.js b/lib/sinon/spy.js index 6e2d193ed..feab6e085 100644 --- a/lib/sinon/spy.js +++ b/lib/sinon/spy.js @@ -164,20 +164,18 @@ var spyApi = { invoke: function invoke(func, thisValue, args) { var matchings = this.matchingFakes(args); var currentCallId = callId++; - var exception, returnValue, i; + var exception, returnValue; incrementCallCount.call(this); push.call(this.thisValues, thisValue); push.call(this.args, args); push.call(this.callIds, currentCallId); - if (matchings) { - for (i = 0; i < matchings.length; i++) { - incrementCallCount.call(matchings[i]); - push.call(matchings[i].thisValues, thisValue); - push.call(matchings[i].args, args); - push.call(matchings[i].callIds, currentCallId); - } - } + matchings.forEach(function (matching) { + incrementCallCount.call(matching); + push.call(matching.thisValues, thisValue); + push.call(matching.args, args); + push.call(matching.callIds, currentCallId); + }); // Make call properties available from within the spied function: createCallProperties.call(this); @@ -199,12 +197,10 @@ var spyApi = { push.call(this.exceptions, exception); push.call(this.returnValues, returnValue); - if (matchings) { - for (i = 0; i < matchings.length; i++) { - push.call(matchings[i].exceptions, exception); - push.call(matchings[i].returnValues, returnValue); - } - } + matchings.forEach(function (matching) { + push.call(matching.exceptions, exception); + push.call(matching.returnValues, returnValue); + }); var err = new ErrorConstructor(); // 1. Please do not get stack at this point. It may be so very slow, and not actually used @@ -214,11 +210,9 @@ var spyApi = { throw err; } catch (e) {/* empty */} push.call(this.errorsWithCallStack, err); - if (matchings) { - for (i = 0; i < matchings.length; i++) { - push.call(matchings[i].errorsWithCallStack, err); - } - } + matchings.forEach(function (matching) { + push.call(matching.errorsWithCallStack, err); + }); // Make return value and exception available in the calls: createCallProperties.call(this);