From f359ee2ba4e5c0361ec1781374df17ba43d3342d Mon Sep 17 00:00:00 2001 From: Maximilian Antoni Date: Wed, 20 Jun 2012 20:57:53 +0200 Subject: [PATCH] Fix: spy.reset did not reset fakes created by spy.withArgs --- lib/sinon/spy.js | 5 ++++ test/sinon/spy_test.js | 52 ++++++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/lib/sinon/spy.js b/lib/sinon/spy.js index ff23f52d1..964ebf6dd 100644 --- a/lib/sinon/spy.js +++ b/lib/sinon/spy.js @@ -120,6 +120,11 @@ this.thisValues = []; this.exceptions = []; this.callIds = []; + if (this.fakes) { + for (var i = 0; i < this.fakes.length; i++) { + this.fakes[i].reset(); + } + } }, create: function create(func) { diff --git a/test/sinon/spy_test.js b/test/sinon/spy_test.js index d3bdb719c..44d69ed71 100644 --- a/test/sinon/spy_test.js +++ b/test/sinon/spy_test.js @@ -2282,13 +2282,8 @@ if (typeof require === "function" && typeof module === "object") { } }, - "reset": { - "resets spy state": function () { - var spy = sinon.spy(); - spy(); - - spy.reset(); - + "reset": (function () { + function assertReset(spy) { assert(!spy.called); assert(!spy.calledOnce); assert.equals(spy.args.length, 0); @@ -2299,18 +2294,45 @@ if (typeof require === "function" && typeof module === "object") { assert.isNull(spy.secondCall); assert.isNull(spy.thirdCall); assert.isNull(spy.lastCall); - }, + } + + return { + "resets spy state": function () { + var spy = sinon.spy(); + spy(); - "resets call order state": function () { - var spies = [sinon.spy(), sinon.spy()]; - spies[0](); - spies[1](); + spy.reset(); - spies[0].reset(); + assertReset(spy); + }, - assert(!spies[0].calledBefore(spies[1])); + "resets call order state": function () { + var spies = [sinon.spy(), sinon.spy()]; + spies[0](); + spies[1](); + + spies[0].reset(); + + assert(!spies[0].calledBefore(spies[1])); + }, + + "resets fakes returned by withArgs": function () { + var spy = sinon.spy(); + var fakeA = spy.withArgs("a"); + var fakeB = spy.withArgs("b"); + spy("a"); + spy("b"); + spy("c"); + var fakeC = spy.withArgs("c"); + + spy.reset(); + + assertReset(fakeA); + assertReset(fakeB); + assertReset(fakeC); + } } - }, + }()), "withArgs": { "defines withArgs method": function () {