Я пытаюсь выполнить модульное тестирование перехватчика, используя приведенный ниже код, но я получаю исключение, помогите мне, пожалуйста, написать мой код в stackbliz
https://stackblitz.com/edit/angular-unit-testing-example-xnm226
перехватчик
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status === 401) {
}
const error = err.error.message || err.statusText;
return throwError(error);
}))
}
}
тест
import { TestBed, inject, async } from "@angular/core/testing";
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HTTP_INTERCEPTORS, HttpClient, HttpHeaders, HttpRequest, HttpResponse, HttpClientModule, HttpErrorResponse, HttpHandler } from '@angular/common/http';
import { AutherizationInterceptor, InterceptorSkipHeader } from './authInterceptors';
import { ErrorInterceptor } from './errorInterceptors';
import { environment } from 'src/environments/environment';
describe('ErrorInterceptor', () => {
let http: HttpTestingController;
let httpClient: HttpClient;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, HttpClientTestingModule],
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: ErrorInterceptor,
multi: true
}]
})
http = TestBed.get(HttpTestingController);
httpClient = TestBed.get(HttpClient);
});
it('Error interceptor test', () => {
httpClient.get(environment.baseUrl + 'api/user/login').subscribe(
response => {
expect(response).toBeTruthy();
}
);
http.expectOne(environment.baseUrl + 'api/user/login').error(new ErrorEvent('Unauthorized error'), {
status: 401
});
http.verify();
});
});