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

351
Visualizações
How to stub an async database call with sinon

I am trying to unit test the getAll method on this class:

module.exports = class BookController {
  static async getAll() {
    return await Book.query()
      .eager('reviews');
  }
};

Book is a Objection model.

How I'd like to test it is I want to fake the response from Book.query().eager() with my own data, since I can never verify what's in the database. For it to be a true unit test, should I just be testing that the Book.query() method is called? Or should I be testing that the returned data since that's the contract of the getAll() method? I'm really unsure how I should be stubbing this out.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

With Sinon > 2.x, you can call .resolves() on your stub. I would do something like

var stub = sinon.stub(BookController, 'getAll');
stub.resolves({response:"ok"});
var bc = new BookController();
bc.getAll.then(function(data){
    expect(data.response).to.equal("ok");
    done();
},function(err){
    done("should NEVER get here");
});
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