Рассмотрим следующий Http-перехватчик:
export class BusyIndicatorInterceptor implements HttpInterceptor {
constructor(private busyIndicatorService: BusyIndicatorService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.busyIndicatorService.show();
return next.handle(req).pipe(tap((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
this.busyIndicatorService.hide();
}
}, err => {
console.warn(err);
this.busyIndicatorService.hide();
}));
}
}
Я пытаюсь провести модульный тест, который вызывает busyIndicatorService.hide
при возникновении ошибки, такой как 400
, 404
и т. Д. Я написалследующий тест, но я думаю, что я не издеваюсь над обработчиком должным образом.
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpErrorResponse({ error: "Not Found", status: 404 })));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
Результат:
Expected spy BusyIndicatorService.hide to have been called once. It was called 0 times
Следующий тест работает для проверки блока успеха, где я проверяю обработчик для возвратановый HttpResponse.
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpResponse()));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
Вот полный тестовый класс:
describe("BusyIndicatorInterceptor", () => {
let testee: BusyIndicatorInterceptor;
let busyIndicatorService: any;
let mockRequest: any;
let mockhandler: any;
beforeEach(() => {
mockRequest = jasmine.createSpyObj("HttpRequest", [""]);
mockhandler = jasmine.createSpyObj("HttpHandler", ["handle"]);
busyIndicatorService = jasmine.createSpyObj("BusyIndicatorService", ["show", "hide"]);
busyIndicatorService.visibility = jasmine.createSpyObj("BehaviourSubject<boolean>", ["next"]);
testee = new BusyIndicatorInterceptor(busyIndicatorService);
});
it("should be created", () => {
expect(testee).toBeTruthy();
});
describe("intercept", () => {
it("should set busy indicator visibility to true", () => {
mockhandler.handle.and.returnValue(of(null));
testee.intercept(mockRequest, mockhandler);
expect(busyIndicatorService.show).toHaveBeenCalledTimes(1);
});
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpResponse()));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpErrorResponse({ error: "Not Found", status: 404 })));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
});
});