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

352
Vistas
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 Respuestas
Responde la pregunta

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 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