vendredi 11 octobre 2019

Assistance stubbing a function inside class

I am trying to stub Enmap's set method. This is my function (inside of my Queue class):

// save queue for persistence
  save() {
    enmap.set('queue', this._queue);
}

This is what I have done so far:

var enmapStub;
  beforeEach(() => {
    enmapStub = sinon.stub(new enmap(), 'set');
  });

  afterEach(() => {
    enmapStub.restore();
  });

Then using it in my test:

describe('#save', () => {
    it("calls enmap.set", () => {
      new Queue({ queueName: 'test', queue: [1,2,3] }).save();
      expect(enmapStub).to.have.been.calledOnce;
    });
  });

The test fails because enmapStub has not been called?

I am new to using sinon and mocking in general so I'm sure I missed a step or something. Does anyone know where I went wrong?

Aucun commentaire:

Enregistrer un commentaire