小编Dou*_*Jr.的帖子

如何对从管道可观察到的错误进行捕获的Angular打字稿Http错误拦截器进行单元测试?

我正在运行一个实验,通过测试其他人的代码来学习角度和打字稿(例如自动化单元测试和端到端测试)。经过测试后,我计划将其重新用于我正在大学教室中进行的宠物项目。

我至少已经从此处对代码进行单元测试了一半:http : //jasonwatmore.com/post/2018/05/16/angular-6-user-registration-and-login-example-tutorial

我一直在尝试下面的代码进行单元测试,但是到目前为止,我根据自己的想法或互联网上的想法尝试过的一切都没有成功:

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http";
import { AuthenticationService } from "src/app/authenticationService/AuthenticationService";
import { Observable, throwError } from "rxjs";
import { catchError } from "rxjs/operators";
import { Injectable } from "@angular/core";

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    constructor(private authenticationService: AuthenticationService) {}

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        console.log('before error handle')
        return next.handle(request).pipe(catchError(err => {

            console.log('in error handle')

            if (err.status === 401) { 
                // auto logout if 401 response returned from …
Run Code Online (Sandbox Code Playgroud)

unit-testing rxjs typescript angular

6
推荐指数
1
解决办法
1145
查看次数

标签 统计

angular ×1

rxjs ×1

typescript ×1

unit-testing ×1