Skip to content

Commit

Permalink
Fix #1372: make sandbox.resetHistory also reset spies
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed May 27, 2017
1 parent ebe37da commit e55de8d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/sinon/collection.js
Expand Up @@ -46,7 +46,16 @@ var collection = {
},

resetHistory: function resetHistory() {
each(this, "resetHistory");
getFakes(this).forEach(function (fake) {
if (typeof fake.resetHistory === "function") {
fake.resetHistory();
return;
}

if (typeof fake.reset === "function") {
fake.reset();
}
});
},

verifyAndRestore: function verifyAndRestore() {
Expand Down
10 changes: 9 additions & 1 deletion test/collection-test.js
Expand Up @@ -365,20 +365,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 @@ -180,4 +180,21 @@ describe("issues", function () {
assert(firstFake !== secondFake);
});
});

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
});
});
});

0 comments on commit e55de8d

Please sign in to comment.