问题
In Angular 7.x I have a global error handling that injects his services with the Injector. So each function has a reference with the injector, like so:
import { ErrorHandler, Injectable, Injector, NgZone } from '@angular/core';
import { Router } from '@angular/router';
import { LoggingService } from '../logging/logging.service';
import { EnvironmentService } from '../services/environment.service';
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor(private readonly injector: Injector, private readonly zone: NgZone) {}
handleError(error: any): void {
// Handle Client Error (Angular Error, ReferenceError...)
this.processError(error);
throw error;
}
processError(error: any): void {
const environmentService = this.injector.get(EnvironmentService);
const environment = environmentService.getEnvironment();
if (!environment.production) {
console.error(error);
} else {
// Log the expection to the logger
this.logException(error);
this.zone.run(() => {
this.navigateToErrorPage(error);
});
}
}
private logException(error: any): void {
const loggingService = this.injector.get(LoggingService);
loggingService.logException(error);
}
private navigateToErrorPage(error: any): void {
const router = this.injector.get(Router);
router.navigate(['/500'], { queryParams: { error } });
}
}
As you can see, in the processError
function I inject the environment service. The only goal for this service is to be able to mock the environment in my spec tests. I do this in another service test to but I use that with dependency injection and not with the this.injector.get(...)
function.
Does anyone know how I mock this?
it('should log the error if the environment is in production', () => {
// Arrange
const environmentSpy = jasmine.createSpyObj('EnvironmentService', 'getEnvironment'); ??? How do I mock this ???
const error: Error = new Error('New Error');
spyOn<any>(errorHandler, 'logException');
// Act
errorHandler.processError(error);
// Assert
expect(errorHandler['logException']).toHaveBeenCalledWith(error);
});
回答1:
You can spy on the Injector and return a fake class in place of the EnvironmentService that has a customized getEnvironment()
method:
spyOn(TestBed.get(Injector), 'get').and.callFake((token) => {
if (token === EnvironmentService) {
// Return a mocked EnvironmentService class
return {
getEnvironment: () => { return { production: true }; }
};
} else {
// Otherwise, return whatever was originally defined in the TestBed
return TestBed.get(token);
}
});
Alternately, you could use the real Injector and spy on the EnvironmentService instead:
spyOn(TestBed.get(EnvironmentService), 'getEnvironment').and
.returnValue({ production: true });
来源:https://stackoverflow.com/questions/55444904/how-to-mock-a-injected-service-in-a-function-with-injector