Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
Sinon: force callback call

I'm testing a function with this code:

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

I want to test the path of the fail, i.e., when the findById method calls the callback with an error. I'm using sinon to generate a stub for my repository and its findById method, but I don't know how to force the stub to call the callback with the desired parameters

Anybody did something like that before?

Thanks

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

With Sinon 2, you can use the callsFake method of a stub:

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

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

over 4 years ago · Santiago Trujillo Denunciar

0

A more generic answer here: Every time I have to stub a callback, I do it like this

const stubFindId = sinon.stub(repository, 'findById');    
stubGetitem.callsFake((value: string, callback: any) => {
      return callback(true, false);
    });
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda