Sinon: call force callback

I am testing a function using this code:

return new Promise((ok, fail) => {
  this.repository.findById(id, (error, result) => {
    if (error)
      return fail(error);
    ok(result);
  });
});

I want to check the path of failure, i.e. when the method findByIdcalls the callback with an error. I use sinon to create a stub for my method repositoryand it findById, but I don't know how to make a stub call a callback with the required parameters

Has anyone done something like this before?

thank

+4
source share
1 answer

With Sinon 2, you can use the callsFakestub method :

sinon.stub(repository, 'findById').callsFake((id, callback) =>
    callback(new Error('oops'))
);

See Sinon 2 documentation: http://sinonjs.org/releases/v2.1.0/stubs/

+5
source

Source: https://habr.com/ru/post/1675908/


All Articles