1

考虑以下 Http 拦截器:

export class BusyIndicatorInterceptor implements HttpInterceptor {
    constructor(private busyIndicatorService: BusyIndicatorService) { }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        this.busyIndicatorService.show();

        return next.handle(req).pipe(tap((event: HttpEvent<any>) => {
            if (event instanceof HttpResponse) {
                this.busyIndicatorService.hide();
            }
        }, err => {
            console.warn(err);
            this.busyIndicatorService.hide();
        }));
    }
}

我正在尝试对busyIndicatorService.hide发生错误时调用的进行单元测试,例如400404。我编写了以下测试,但我认为我没有正确地模拟处理程序。

    it("should set busy indicator visibility to false when HttpResponse is returned", () => {
        mockhandler.handle.and.returnValue(of(new HttpErrorResponse({ error: "Not Found", status: 404 })));
        const result = testee.intercept(mockRequest, mockhandler);
        result.subscribe(() => {
            expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
        });
    });

结果:

Expected spy BusyIndicatorService.hide to have been called once. It was called 0 times

以下测试用于测试成功块,我在其中模拟处理程序以返回新的 HttpResponse。

it("should set busy indicator visibility to false when HttpResponse is returned", () => {
    mockhandler.handle.and.returnValue(of(new HttpResponse()));
    const result = testee.intercept(mockRequest, mockhandler);
    result.subscribe(() => {
        expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
    });
});

这是完整的测试类:

describe("BusyIndicatorInterceptor", () => {
    let testee: BusyIndicatorInterceptor;
    let busyIndicatorService: any;
    let mockRequest: any;
    let mockhandler: any;

    beforeEach(() => {
        mockRequest = jasmine.createSpyObj("HttpRequest", [""]);
        mockhandler = jasmine.createSpyObj("HttpHandler", ["handle"]);
        busyIndicatorService = jasmine.createSpyObj("BusyIndicatorService", ["show", "hide"]);
        busyIndicatorService.visibility = jasmine.createSpyObj("BehaviourSubject<boolean>", ["next"]);
        testee = new BusyIndicatorInterceptor(busyIndicatorService);
    });

    it("should be created", () => {
        expect(testee).toBeTruthy();
    });

    describe("intercept", () => {

        it("should set busy indicator visibility to true", () => {
            mockhandler.handle.and.returnValue(of(null));
            testee.intercept(mockRequest, mockhandler);

            expect(busyIndicatorService.show).toHaveBeenCalledTimes(1);
        });

        it("should set busy indicator visibility to false when HttpResponse is returned", () => {
            mockhandler.handle.and.returnValue(of(new HttpResponse()));
            const result = testee.intercept(mockRequest, mockhandler);
            result.subscribe(() => {
                expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
            });
        });

        it("should set busy indicator visibility to false when HttpResponse is returned", () => {
            mockhandler.handle.and.returnValue(of(new HttpErrorResponse({ error: "Not Found", status: 404 })));
            const result = testee.intercept(mockRequest, mockhandler);
            result.subscribe(() => {
                expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
            });
        });
    });
});
4

0 回答 0