In the process of testing an Angular 1 application using Jasmine, I have encountered a dilemma. My question is, can two spies be created for two different services within the same beforeEach
statement?
Currently, I have managed to make the first spy work as expected. However, I am struggling to understand why the second spy is not functioning properly. The setup involves assigning promises to global variables inside the spies so that they can be accessed in subsequent tests. Unfortunately, the second variable is returning an unexpected value of undefined
instead of the intended promise.
Below is the snippet of code from the beforeEach
block:
mockWorkingService = jasmine.createSpyObj('mockWorkingService', ['retrieve']);
mockWorkingService.retrieve.and.callFake(function(crit) {
workingServiceDfr = $q.defer(); // Here, $q is a globally defined object
return workingService.promise;
});
mockFailingService = jasmine.createSpyObj('mockFailingService', ['retrieve']);
mockFailingService.retrieve.and.callFake(function(crit) {
failingServiceDfr = $q.defer();
return failingService.promise;
});
It is worth noting that the method retrieve
is distinctly defined for each service.