From 6d59744bd5ada23bec3df3b69ee2fada5158cf30 Mon Sep 17 00:00:00 2001 From: RealZogger <49367953+RealZogger@users.noreply.github.com> Date: Tue, 14 Jan 2020 21:49:27 +0000 Subject: [PATCH 1/2] Support negative indices in getCall This allows getting calls indexed from the end of the array e.g. -1 gets the last call, -2 gets the penultimate call etc. --- docs/release-source/release/spies.md | 2 ++ lib/sinon/proxy.js | 6 +++- test/spy-test.js | 42 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/docs/release-source/release/spies.md b/docs/release-source/release/spies.md index b77f40fc2..be8a9b995 100644 --- a/docs/release-source/release/spies.md +++ b/docs/release-source/release/spies.md @@ -350,6 +350,8 @@ Returns `true` if spy always returned the provided value. Returns the *nth* [call](#spycall). +If *n* is negative, the *nth* call from the end is returned. For example, `spy.getCall(-1)` returns the last call, and `spy.getCall(-2)` returns the second to last call. + Accessing individual calls helps with more detailed behavior verification when the spy is called more than once.
diff --git a/lib/sinon/proxy.js b/lib/sinon/proxy.js index 7af3fb788..28e7e86d3 100644 --- a/lib/sinon/proxy.js +++ b/lib/sinon/proxy.js @@ -40,7 +40,11 @@ var proxyApi = { return emptyFakes; }, - getCall: function getCall(i) { + getCall: function getCall(index) { + var i = index; + if (i < 0) { + i += this.callCount; + } if (i < 0 || i >= this.callCount) { return null; } diff --git a/test/spy-test.js b/test/spy-test.js index 5c35d9d4d..3bbf50bab 100644 --- a/test/spy-test.js +++ b/test/spy-test.js @@ -2086,6 +2086,48 @@ describe("spy", function() { }); }); + describe(".getCall", function() { + it("is null for indexes >= length", function() { + var spy = createSpy(); + + spy(); + + assert.isNull(spy.getCall(1)); + assert.isNull(spy.getCall(2)); + }); + + it("is null for indexes < -length", function() { + var spy = createSpy(); + + spy(); + + assert.isNull(spy.getCall(-2)); + assert.isNull(spy.getCall(-3)); + }); + + it("is same as last call when passed index -1", function() { + var spy = createSpy(); + + spy(); + spy(); + spy(); + + assert.same(spy.getCall(-1).callId, spy.lastCall.callId); + assert.same(spy.getCall(-1).spy, spy.lastCall.spy); + }); + + it("is same as n-1th call when passed index -2", function() { + var spy = createSpy(); + + spy(); + spy(); + spy(); + + assert.same(spy.getCall(-2).callId, spy.getCall(1).callId); + assert.same(spy.getCall(-2).spy, spy.getCall(1).spy); + }); + }); + describe(".lastCall", function() { it("is undefined by default", function() { var spy = createSpy(); From d851a5ed367faadb97a410a017f14097f986e277 Mon Sep 17 00:00:00 2001 From: RealZogger <49367953+RealZogger@users.noreply.github.com> Date: Wed, 15 Jan 2020 22:17:14 +0000 Subject: [PATCH 2/2] Add clarifying comment for negative indices --- lib/sinon/proxy.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sinon/proxy.js b/lib/sinon/proxy.js index 28e7e86d3..7b7807aad 100644 --- a/lib/sinon/proxy.js +++ b/lib/sinon/proxy.js @@ -43,6 +43,7 @@ var proxyApi = { getCall: function getCall(index) { var i = index; if (i < 0) { + // Negative indices means counting backwards from the last call i += this.callCount; } if (i < 0 || i >= this.callCount) {