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

382
Vistas
Jasmine test case error: spy error to have been called

I have the following code that needs to be tested in Jasmine

sampleMethod() {
  if (this.sampleObject) {
    // business logic
  } else {
    console.error('sampleObject not initialized');
  }
}

I had referred to Spying on console.error() with Jasmine to add jasmine test to monitor console.error.

  it('should check for console.error', () => {
     spyOn(console, 'error');
     component.sampleMethod();
     expect(console.error).toHaveBeenCalled();
  });

However, I'm getting the following error:

Error: Expected spy error to have been called.

SampleObject is undefined. I've also tried adding spyOn to beforeEach. Still, I'm getting the error.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Can you try this to ensure that sampleObject is undefined and try assigning the spy to a variable?

it('should check for console.error', () => {
     const errorSpy = spyOn(console, 'error');
     component.sampleObject = undefined;
     component.sampleMethod();
     expect(errorSpy).toHaveBeenCalled();
  });
about 4 years ago · Santiago Gelvez 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