Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.9K
Views
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 answers
Answer question

0

Puedes sobrescribirlo

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

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!