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

171
Visualizações
How to get toHaveBeenCalledTimes(9) with a nested function in jasmine?

I'm trying to make a unit test to check if a function is called a maximum of 9 times, the following code is an approximation of the real code:

function method1( event )
{
     method2(event);
}

function method3( event )
{
    input.value += event.key;
}

function method2(event)
{
    if (input.length < 9)
        method3(event);
}

As you can see the method3() is supposed to be executed only 9 times.

This is an approximation too of the Unit test I'm trying to do:

it("method3 should be called 9 times",() => {
spyOn(component.method1).and.callThrough;
spyOn(component.method2).and.callThrough;
spyOn(component.method3).and.callThrough;

for (var i = 0; i < 20; i++) {
    component.method1(new KeyboardEvent('keydown', {key:'1'}));
}

expect(component.method1).toHaveBeenCalled();
expect(component.method2).toHaveBeenCalled();
expect(component.method3).toHaveBeenCalled();
expect(component.method3).toHaveBeenCalledTimes(9); // with this the TEST DON'T PASS!
});

After execute the tests, the tests fails in the toHaveBeenCalledTimes, because it says: expected to be 9 times, it was called 0 times. I don't know if the reason is because it is a nested function.

Another thing that ocurred too, is that if I do: expect(component.method1).toHaveBeenCalledTimes(20) instead of the (9) with the method3, that test PASS and it is called 20 times.

Can you help me to solve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are not calling the callThrough() method of the spy. spyOn makes a fake implementation of the method that you want to spy on. Therefore, your call to method2 and method3 never happens from fake method1.

and.callThrough() is used to call the actual method that you are spying on.

In your case, you are not calling the callThrough() method. Instead, you are just referring to callThrough function:

spyOn(component.method1).and.callThrough();//Make the actual call

Why this assertion passesspyOn(component.method1).toHaveBeenCalledTimes(20)?

You are calling the method1 20 times in your for loop. Note that, you are calling the fake implementation 20 times and your test case asserts- Hey, is this method called 20 times?

Karma runner says - Yes, it's called 20 times (fake or real method call, it doesn't matter to him).

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