Angular 如何使用路由测试HttpInterceptor?
我用的是角度8 我试图在HttpInterceptor上进行单元测试,并在它收到致命错误时重定向到错误页面 我如何继续我的测试?我只是对errorHandler方法本身进行测试吗?还是我需要从请求的角度触发它 错误处理程序.interceptor.ts:Angular 如何使用路由测试HttpInterceptor?,angular,testing,jasmine,angular8,Angular,Testing,Jasmine,Angular8,我用的是角度8 我试图在HttpInterceptor上进行单元测试,并在它收到致命错误时重定向到错误页面 我如何继续我的测试?我只是对errorHandler方法本身进行测试吗?还是我需要从请求的角度触发它 错误处理程序.interceptor.ts: export class ErrorHandlerInterceptor implements HttpInterceptor { log: Logger; constructor(private noti: Notification
export class ErrorHandlerInterceptor implements HttpInterceptor {
log: Logger;
constructor(private noti: NotificationCenterService, private router: Router, private http: HttpClient) {
this.log = new Logger(http, 'ErrorHandlerInterceptor');
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(error => this.errorHandler(error)));
}
// Customize the default error handler here if needed
private errorHandler(error: HttpErrorResponse): Observable<HttpEvent<any>> {
const errorMsg = error.error.message || error.error || error.statusText;
if (error.status === undefined || error.status === null || error.status === 500 || error.status === 0) {
//redirect to error page
this.router.navigate(['fatalerror']);
} else {
//show in notification
this.noti.open(errorMsg, 'OK');
}
this.log.info("ErrorHandlerInterceptor - Error not caught!");
throw error;
}
}
...
const routes: Routes = [
Shell.childRoutes([{ path: 'fatalerror', loadChildren: './shared/fatalerror/fatalerror.module#FatalerrorModule' }]),
{ path: '**', redirectTo: '', pathMatch: 'full' }
];
...
import { Type } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { ErrorHandlerInterceptor } from './error-handler.interceptor';
import { NotificationCenterService } from '@app/shared/notification-center.service';
describe('ErrorHandlerInterceptor', () => {
let errorHandlerInterceptor: ErrorHandlerInterceptor;
let http: HttpClient;
let httpMock: HttpTestingController;
let noti: jasmine.SpyObj<NotificationCenterService>;
let router: Router;
function createInterceptor() {
errorHandlerInterceptor = new ErrorHandlerInterceptor(noti, router, http);
return errorHandlerInterceptor;
}
beforeEach(() => {
const spy = jasmine.createSpyObj('NotificationCenterService', ['open']);
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(routes)],
providers: [
{
provide: HTTP_INTERCEPTORS,
useFactory: createInterceptor,
multi: true
},
{
provide: NotificationCenterService,
useValue: spy
}
]
});
noti = TestBed.get(NotificationCenterService);
http = TestBed.get(HttpClient);
httpMock = TestBed.get(HttpTestingController as Type<HttpTestingController>);
router = TestBed.get(Router);
});
afterEach(() => {
httpMock.verify();
});
it('should redirect to fatal error page upon fatal error', () => {
spyOn(ErrorHandlerInterceptor.prototype as any, 'errorHandler').and.callThrough();
//how to proceed
});
});
错误处理.interceptor.spec.ts:
export class ErrorHandlerInterceptor implements HttpInterceptor {
log: Logger;
constructor(private noti: NotificationCenterService, private router: Router, private http: HttpClient) {
this.log = new Logger(http, 'ErrorHandlerInterceptor');
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(error => this.errorHandler(error)));
}
// Customize the default error handler here if needed
private errorHandler(error: HttpErrorResponse): Observable<HttpEvent<any>> {
const errorMsg = error.error.message || error.error || error.statusText;
if (error.status === undefined || error.status === null || error.status === 500 || error.status === 0) {
//redirect to error page
this.router.navigate(['fatalerror']);
} else {
//show in notification
this.noti.open(errorMsg, 'OK');
}
this.log.info("ErrorHandlerInterceptor - Error not caught!");
throw error;
}
}
...
const routes: Routes = [
Shell.childRoutes([{ path: 'fatalerror', loadChildren: './shared/fatalerror/fatalerror.module#FatalerrorModule' }]),
{ path: '**', redirectTo: '', pathMatch: 'full' }
];
...
import { Type } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { ErrorHandlerInterceptor } from './error-handler.interceptor';
import { NotificationCenterService } from '@app/shared/notification-center.service';
describe('ErrorHandlerInterceptor', () => {
let errorHandlerInterceptor: ErrorHandlerInterceptor;
let http: HttpClient;
let httpMock: HttpTestingController;
let noti: jasmine.SpyObj<NotificationCenterService>;
let router: Router;
function createInterceptor() {
errorHandlerInterceptor = new ErrorHandlerInterceptor(noti, router, http);
return errorHandlerInterceptor;
}
beforeEach(() => {
const spy = jasmine.createSpyObj('NotificationCenterService', ['open']);
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(routes)],
providers: [
{
provide: HTTP_INTERCEPTORS,
useFactory: createInterceptor,
multi: true
},
{
provide: NotificationCenterService,
useValue: spy
}
]
});
noti = TestBed.get(NotificationCenterService);
http = TestBed.get(HttpClient);
httpMock = TestBed.get(HttpTestingController as Type<HttpTestingController>);
router = TestBed.get(Router);
});
afterEach(() => {
httpMock.verify();
});
it('should redirect to fatal error page upon fatal error', () => {
spyOn(ErrorHandlerInterceptor.prototype as any, 'errorHandler').and.callThrough();
//how to proceed
});
});
从'@angular/core'导入{Type};
从“@angular/core/testing”导入{TestBed};
从“@angular/common/http/testing”导入{HttpClientTestingModule,HttpTestingController};
从'@angular/common/HTTP'导入{HTTP_拦截器,HttpClient};
从“@angular/router/testing”导入{RouterTestingModule};
从'@angular/Router'导入{Router};
从“@angular/common”导入{Location};
从“./error handler.interceptor”导入{ErrorHandlerInterceptor};
从'@app/shared/notification center.service'导入{NotificationCenterService};
描述('ErrorHandlerInterceptor',()=>{
let errorHandlerInterceptor:errorHandlerInterceptor;
让http:HttpClient;
让httpock:HttpTestingController;
别提了,茉莉花;
让路由器:路由器;
函数createInterceptor(){
errorHandlerInterceptor=新的errorHandlerInterceptor(noti、路由器、http);
返回错误句柄拦截器;
}
在每个之前(()=>{
const spy=jasmine.createSpyObj('NotificationCenterService',['open']);
TestBed.configureTestingModule({
导入:[HttpClientTestingModule,RouterTestingModule.withRoutes(routes)],
供应商:[
{
提供:HTTP_拦截器,
useFactory:createInterceptor,
多:真的
},
{
提供:通知中心服务,
使用价值:间谍
}
]
});
noti=TestBed.get(NotificationCenterService);
http=TestBed.get(HttpClient);
httpMock=TestBed.get(类型为HttpTestingController);
路由器=测试台.get(路由器);
});
之后(()=>{
httpMock.verify();
});
它('发生致命错误时应重定向到致命错误页',()=>{
spyOn(ErrorHandlerInterceptor.prototype作为任何“errorHandler”)。和.callThrough();
//如何进行
});
});