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

108
Visualizações
How to test a service written using Partials

I have the following service and I want to write Unit tests for this service. However, I am exporting a function that is going to return the Service.

My main problem is, to keep things simple at the point where this service will be imported and used, I am using partials to pass in the model as dependency using Partials inside the init method.

const model = require('../models');

const SomeService = (model) => {
  const getSomething = (id) => {
    const [data, error] = model.getSomething(id);
    if (error) {
      return error;
    }
    return data;
  };

  const createSomething = (smt) => {
    const [data, error] = model.createSomething(smt);
    if (error) {
      return error;
    }
    return data;
  };

  const deleteSomething = (id) => {
    const [data, error] = model.deleteSomething(id);
    if (error) {
      return error;
    }
    return data;
  };

  return {
    getSomething,
    createSomething,
    deleteSomething,
  };
};

const init = (model) => () => SomeService(model);

const getSomeService = init(model);

module.exports = getSomeService;

The controller that will be consuming this service may or may not pass some other params but the thing is it definitely should not be passing the model to initialize this service. Hence, I am using the partials.

While writing Unit tests for this service, how do I mock the model? Are there any changes required to make this more testable?

about 4 years ago · Juan Pablo Isaza
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