Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda