Nocomm Nocomm - 1 year ago 60
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


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
instead of the expected promise.

Here is the sample set up code inside the

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;

is defined separately for each service.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download