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

153
Visualizações
Stub object naming conventions

I need to perform a suite of unit tests to a function that sorts an array based on a combination of 4 parameters. The nature of the functions makes it so that it has a lot of edge cases and I need to test all of them.

For this, I'm planning to create a set of Stub arrays both for the inputs and the outputs of the function. But I'm having trouble naming these stub objects.

My first thought was having 2 stubs for each test case, one for the initial array and other for the sorted array. Maybe doing something like this:

//test case: should prioritize duration over raw priority

export const DifferentPriorityAndDurationStub = () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t1, t2];
};

export const DifferentPriorityAndDurationStubResult= () => {
  const t1 = {priority: 1, duration: 20};
  const t2 = {priority: 2, duration: 30};
  return [t2, t1];
};

Then, in the actual test:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const originalArray = DifferentPriorityAndDurationStub();
      const result = DifferentPriorityAndDurationStubResult();
      expect(customSorter.sort( originalArray )).toEqual(result );
    });
  });

My problem with this approach is that if someone needs to work on the function, the test's code would not tell them exactly what is in the inputs and what is in the output, they would need to go to the stubs file. I think this makes it difficult to understand how the sort functions should work in this specific case.

The other way I thought about was something like:

export const Priority1Duration20 = () => {
  return {priority: 1, duration: 20};
};

export const Priority2Duration30 = () => {
  return {priority: 2, duration: 30};
};

And in my test do something like this:

describe('Custom Sorter', () => {
    it('should prioritize duration over raw priority', () => {
      const Priority1Duration20 = Priority1Duration20();
      const Priority2Duration30 = Priority2Duration30();
      expect(customSorter.sort( [Priority1Duration20, Priority2Duration30] ))
            .toEqual([Priority2Duration30,Priority1Duration20]);
    });
  });

I think this seems more readable if someone needs to understand how the code works. But it also worries me that I need to make changes to the Stubs file and the test file to change or fix certain test cases. In the first approach I'd just need to fix the stub and that would be all.

Any ideas? how would you approach this?

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