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

1.9K
Vistas
Pruebas de unidades angulares con Jasmine: cómo eliminar o modificar spyOn

AngularJS v1.2.26

Jazmín v2.2.0

¿Cómo puedo cambiar o eliminar el comportamiento de un spyOn ? Cuando intento anularlo, aparece el siguiente error: Error: getUpdate has already been spied upon

 var data1 = 'foo'; var data2 = 'bar'; describe("a spec with a spy", function(){ beforeEach(module('app')); var $q; beforeEach(inject(function(_updateService_, _$q_){ updateService = _updateService_; //spy the results of the getUpdate() $q = _$q_; var deferred = $q.defer(); deferred.resolve( data1 ); spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise); })); describe('and here the spy should be different', function() { it('returns a different value', function() { var deferred = $q.defer(); deferred.resolve( data2 ); spyOn(updateService, 'getUpdate'); //ERROR HERE updateService.getUpdate.and.returnValue(deferred.promise); ... }); }); ...

Cuando elimino el segundo spyOn, la prueba no funciona.

¿Cómo hago esto?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Puedes sobrescribirlo

 updateService.getUpdate = jasmine.createSpy().and.returnValue(etc)
over 4 years ago · Santiago Trujillo Denunciar

0

Puede anular el valor de retorno del espía

 var deferred = $q.defer(); deferred.resolve( data1 ); var getUpdateSpy = spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise); var newDeferred = $q.defer(); newDeferred.resolve( data2 ); getUpdateSpy.and.returnValue(newDeferred.promise);
over 4 years ago · Santiago Trujillo Denunciar

0

Desde jasmine v2.5, use la configuración global allowRespy() .

 jasmine.getEnv().allowRespy(true);

Podrá llamar a spyOn() varias veces, cuando no quiera o no tenga acceso al primer espía. Tenga cuidado, devolverá el espía anterior, si alguno ya está activo.

 spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise); ... spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);

over 4 years ago · Santiago Trujillo 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