角度4で提供されているHttpInterceptorのテスト方法を教えてもらえますか?例に応じてインターセプターを作成しましたが、テスト方法は不明です。以下は私のインターセプターで、カスタムヘッダーが追加されているかどうか、そして応答ステータスが401の場合にテストしたいと思います。window.location.hrefが完了しました。ユニットテストHttpInterceptor from Angular 4
export class MyInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const headers = new HttpHeaders();
this.addHeader(headers); // This will add headers
const changedReq = req.clone({ headers: headers });
return next.handle(req)
.catch(err => {
if (err instanceof HttpErrorResponse) {
switch (err.status) {
case 302:
case 401:
window.location.href = "http//google.com";
break;
default:
throw new Error(this.getErrorMessage(err));
}
}
return Observable.throw(err);
});
}
ありがとうございますが、私はこの場合yourMockが何であるか分かりません。 http.getを呼び出して、インターセプタがヘッダーを追加したかどうかと、レスポンスを疑似したいかどうかを確認したいと思います。 – Angad
をスキップすることができます。あなたが何かを嘲笑していない場合は必要ありません。 –