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

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

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