Angular unit testing with Jasmine: how to remove or modify spyOn
You can just overwrite it
updateService.getUpdate = jasmine.createSpy().and.returnValue(etc)
You can override the return value of the spy
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);
Since jasmine v2.5, use the global allowRespy()
setting.
jasmine.getEnv().allowRespy(true);
You'll be able to call spyOn()
multiple times, when you don't want and/or have access to the first spy. Beware it will return the previous spy, if any is already active.
spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);...spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);