Reset "called" Count on Sinon Spy

28,039

This question was asked a while back but may still be interesting, especially for people who are new to sinon.

this.spied.reset() is not needed as Obj.prototype.spiedMethod.restore(); removes the spy.

Update 2018-03-22:

As pointed out in some of the comments below my answer, stub.reset will do two things:

  1. Remove the stub behaviour
  2. Remove the stub history (callCount).

According to the docs this behaviour was added in [email protected].

The updated answer to the question would be to use stub.resetHistory().

Example from the docs:

var stub = sinon.stub();

stub.called // false

stub();

stub.called // true

stub.resetHistory();

stub.called // false

Update:

  • If you just want to reset the call count, use reset. This keeps the spy.
  • To remove the spy use restore.

When working with sinon you can use the sinon assertions for enhanced testing. So instead of writing expect(this.spied).to.have.been.calledOnce; one could write:

sinon.assert.calledOnce(Obj.prototype.spiedMethod);

This would work as well with this.spied:

sinon.assert.calledOnce(this.spied);

There are a lot of other sinon assertion methods. Next to calledOnce there are also calledTwice, calledWith, neverCalledWith and a lot more on sinon assertions.

Share:
28,039

Related videos on Youtube

cantera
Author by

cantera

Updated on July 09, 2022

Comments

  • cantera
    cantera almost 2 years

    How do I reset the "called" count on a Sinon spy before each test?

    Here's what I'm doing now:

    beforeEach(function() {
      this.spied = sinon.spy(Obj.prototype, 'spiedMethod');
    });
    
    afterEach(function() {
      Obj.prototype.spiedMethod.restore();
      this.spied.reset();
    });
    

    But when I check the call count in a test:

    it('calls the method once', function() {
      $.publish('event:trigger');
      expect(this.spied).to.have.been.calledOnce;
    });
    

    ...the test fails and reports that the method was called X number of times (once for each previous test that also triggered the same event).

    • oak
      oak about 10 years
      spiedObject.reset() works for me. maybe the issue is because you do restore?
  • dman
    dman about 9 years
    spiedObject.reset() seems a cleaner way to do this.
  • T. Junghans
    T. Junghans almost 9 years
    @dman Now that you mention it, I re-read the question and to reset the call count reset is correct, as you may want to keep the spy.
  • Costin
    Costin over 6 years
    Warning (?): There is a side effect. When reseting with stub.reset() it also resets the .returns(). I would have loved to keep the returns and only reset the number of calls. Edit: Use stub.resetHistory() to reset only the counters!
  • m1uan
    m1uan over 6 years
    stub.reset() also remove stub.callsFake((args)=>{...}) so stub.resetHistory() seems better options for remove just the call count
  • Yonggoo Noh
    Yonggoo Noh almost 5 years
    I'm sure that spy.reset() method is deprecated now. If you want to reset the state of the spy, spy.resetHistory() would be better instead of spy.reset().