Skip to content

Commit

Permalink
Fix #1372: make sandbox.resetHistory also reset spies (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick authored and mantoni committed Jun 10, 2017
1 parent bade318 commit 330101a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/sinon/collection.js
Expand Up @@ -46,7 +46,13 @@ var collection = {
},

resetHistory: function resetHistory() {
each(this, "resetHistory");
getFakes(this).forEach(function (fake) {
var method = fake.resetHistory || fake.reset;

if (method) {
method.call(fake);
}
});
},

verifyAndRestore: function verifyAndRestore() {
Expand Down
10 changes: 9 additions & 1 deletion test/collection-test.js
Expand Up @@ -369,20 +369,28 @@ describe("collection", function () {
beforeEach(function () {
this.collection = Object.create(sinonCollection);
this.collection.fakes = [{
// this fake has a resetHistory method
resetHistory: sinonSpy()
}, {
// this fake has a resetHistory method
resetHistory: sinonSpy()
}, {
// this fake pretends to be a spy, which does not have resetHistory method
// but has a reset method
reset: sinonSpy()
}];
});

it("calls resetHistory on all fakes", function () {
it("resets the history on all fakes", function () {
var fake0 = this.collection.fakes[0];
var fake1 = this.collection.fakes[1];
var fake2 = this.collection.fakes[2];

this.collection.resetHistory();

assert(fake0.resetHistory.called);
assert(fake1.resetHistory.called);
assert(fake2.reset.called);
});
});

Expand Down
17 changes: 17 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -217,6 +217,23 @@ describe("issues", function () {
});
});

describe("#1372 - sandbox.resetHistory", function () {
it("should reset spies", function () {
var spy = this.sandbox.spy();

spy();
assert.equals(spy.callCount, 1);

spy();
assert.equals(spy.callCount, 2);

this.sandbox.resetHistory();

spy();
assert.equals(spy.callCount, 1); // should not fail but fails
});
});

describe("#1398", function () {
it("Call order takes into account both calledBefore and callCount", function () {
var s1 = sinon.spy();
Expand Down

0 comments on commit 330101a

Please sign in to comment.