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

298
Visualizações
TDD: How to unit test functions that rely on other functions?

Suppose I have a function A that takes in a string. Function A calls function B from the same file, which takes that string and modifies it. Function A then returns a value based on the modified output string from function B.

How should I best write unit tests for function A given that it depends on the output from B? If my understanding is correct, unit tests should test the logic of a function independent from its dependencies. Following this line of thought, I was thinking of mocking function B and its output so that the test for A wouldn't necessarily fail if function B wasn't working. Is this line of thought correct? Example case below:

const functionA = (string, table) => {
  const hex = functionB(string);

  return table[hex];
}

const functionB = (string) => {
  //some kind of hexing function here

  return hexed string
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The usual answer is to just write your tests for A without mocking B.

The explanation being that "testing a function independent of its dependencies" wasn't actually a priority. Beck, Jeffries, et al used the spelling "Unit Test", but they didn't mean that in the usual sense. Later on, they tried "developer test" and "programmer test" as labels, but by then habit had set in and the damage had been done.

One way to see that this must be true is to consider writing tests for a "unit", and then later performing a refactoring that extracts another method -- the original tests still work, even though the test subject is no longer a single "unit".


There are, of course, cases where you will want to introduce a substitute implementation for B when testing A

  • the real B makes A expensive to test
  • the behaviors of the real B are unstable, making the A tests expensive to maintain
  • the design you are trying to drive is the protocol between A and B

(Your concern appears to be the middle one).


Another possible approach is to extract another function from A

const functionA = (string, table) => {
  return functionC(string, table, functionB);
}

const functionB = (string) => {
  //some kind of hexing function here

  return hexed string
}

const functionC = (string, table, fn) {
  const hex = fn(string);
  return table[hex];
}

Here, functionA is "so simple there are obviously no deficiencies"; we've moved all of the risk to functionC. The "stub" / "test-double" that we want to use as a substitute for functionB is just another argument.

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