Nocomm Nocomm - 9 days ago 5
AngularJS Question

Can you have multiple spyObjs in a beforeEach

I am testing an Angular 1 application with Jasmine. My question is, is it possible to create two spies for two separate services in the same

beforeEach
statement?

Right now I can get the first spy to work, but I'm not sure why the other spy isn't working. I have the spies setup to where a promise is assigned to a global variable inside of them, which can be accessed in any of the tests. So, the second variable is just returning as
undefined
instead of the expected promise.

Here is the sample set up code inside the
beforeEach
:

mockWorkingService = jasmine.createSpyObj('mockWorkingService', ['retrieve']);
mockWorkingService.retrieve.and.callFake(function(crit) {
workingServiceDfr = $q.defer(); // $q is defined globally
return workingService.promise;
});

mockFailingService = jasmine.createSpyObj('mockFailingService', ['retrieve']);
mockFailingService.retrieve.and.callFake(function(crit) {
failingServiceDfr = $q.defer();
return failingService.promise;
});


Also,
retrieve
is defined separately for each service.

Answer

The short answer is "yes", you can have multiple spies in beforeEach statements.