Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sandbox.resetHistory() doesn't reset history of spies #1372

Closed
Flarna opened this issue Mar 31, 2017 · 4 comments · Fixed by #1424
Closed

sandbox.resetHistory() doesn't reset history of spies #1372

Flarna opened this issue Mar 31, 2017 · 4 comments · Fixed by #1424

Comments

@Flarna
Copy link
Contributor

Flarna commented Mar 31, 2017

  • Sinon version : 2.1.0
  • Environment : NodeJs 4.8.1
  • Other libraries you are using: mocha

What did you expect to happen?
sandbox.resetHistory() should reset history of spies.

What actually happens
sandbox.resetHistory() is not reseting history of spies.

How to reproduce

it("reset spy", function(): void {
  const sb = Sinon.sandbox.create();
  const spy = sb.spy();
  spy();
  Assert.equal(spy.callCount, 1);
  spy();
  Assert.equal(spy.callCount, 2);
  sb.resetHistory();
  spy();
  Assert.equal(spy.callCount, 3);  // should fail but doesn't
  Assert.equal(spy.callCount, 1);  // should not fail but fails
});
@lucasfcosta
Copy link
Member

lucasfcosta commented Apr 24, 2017

Hi @Flarna, thanks for your issue!

You're using the wrong method to reset spies' history.

You should use sandbox.reset() instead of `sandbox.resetHistory().

According to the docs:

sandbox.resetHistory();
Resets the history of all stubs created through the sandbox

Since you want to restore spies and not stubs you should use:

sandbox.reset();
Resets the internal state of all fakes created through sandbox.

This happens because sandbox.resetHistory calls the resetHistory method for each one of its fakes, but, since these are spies, they only have the reset method.

Let me know if you have any further doubts, otherwise we can wait for a maintainer to close this issue if they believe it's solved.

@Flarna
Copy link
Contributor Author

Flarna commented Apr 25, 2017

If I use sandbox.reset() the history of spies is reseted but additionally the behavior of all stubs which is unfortunately not what I need.
I feel that resetting history and behavior should be separated everywhere.
Sure, spies have no behavior so I guess reset and resetHistory is effectively the same here.

@dslagle
Copy link

dslagle commented May 17, 2017

I just ran into this issue today and would agree with @Flarna. I expected sandbox.resetHistory() to also reset the history of the spies. It seems like overkill to need to use sandbox.reset() and then have to rebuild the behavior of all my stubs.

@mroderick
Copy link
Member

The fix has been released as v2.3.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants