Попробуйте что-то вроде этого, надеюсь, это поможет.
import { HttpErrorResponse} from '@angular/common/http';
import { ErrorInterceptor} from './ErrorInterceptor';
import { of } from 'rxjs';
import { take } from 'rxjs/operators';
import { HttpErrorResponse} from '@angular/common/http';
import { AuthenticationService} from '....';
describe('ErrorInterceptor', () => {
let interceptor: ErrorInterceptor;
let mockAuthenticationService: AuthenticationService;
beforeEach(() => {
mockAuthenticationService = {
logout: jasmine.createSpy('logout')
};
mockRouter = {
navigate: jasmine.createSpy('navigate')
};
interceptor = new ErrorInterceptor(mockAuthenticationService as any, mockRouter as any);
});
describe('intercept', () => {
beforeEach(() => {
const payload = {
statue: 401
},
response = new HttpErrorResponse(payload),
next: any = {
handle: jasmine.createSpy('handle').and.callFake(() => of(response))
};
interceptor.intercept(response as any, next).pipe(take(1))
.subscribe();
});
describe('when status is 401', () => {
it('should logout',() => {
expect(mockAuthenticationService.logout).toHaveBeenCalled();
expect(mockRouter.navigate).toHaveBeenCalledWith([`\logout`]);
});
});
...
});