-1

I have created a spec for my library to ensure that a value is emitted at a regular interval. I am using sinonjs and have created a callback as a spy. I am using the fake timer in sinonjs to simulate an extra two intervals of 10 seconds passed. However when the test lands on the first use of the tick method, this is when the error is emitted. I produces the below error

ExpectationError: Unexpected call: getTheValue()
Expectation met: getTheValue([...]) once

Below is the code for my test

it('should emit the values at an interval', function () {
  var callback = this.sandbox.spy();
  var interval = this.sandbox.useFakeTimers();

  this.myLib.emitValues(callback);

  interval.tick(1000);
  interval.tick(1000);

  callback.should.have.been.calledWith('test');
});

This is my production code

_getValue() {
  var value = getTheValue(this.id);

  this.myListener(value);
}

emitValues(callback) {
  this.myListener = callback;

  this._getValue();
  setInterval(() => this._getValue(), 1000);
}

Does anyone have any idea why I might be getting this error?

1 Answers1

1

If you expect the callback to be called various times, use calledWith or calledWithExactly and provide various parameters for each call that was expected.

sinon-chai is just API wrapper on top of Sinon. So you can read more about these expectations in Sinon docs.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92