簡體   English   中英

單元測試角度HttpInterceptor重試

[英]Unit Testing angular HttpInterceptor retry

這是我嘗試進行角度的單元測試的第一個項目,因此我只是弄清楚了它是如何工作的。 該項目的角度為7,如果HTTP請求失敗,我有一個HttpInteceptorService重試2次:

@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
    constructor() { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
        return next.handle(request).pipe(retry(2));
    }
}

到目前為止,我對該攔截器的測試:

describe('HttpInterceptorService', () => {
    beforeEach(() => TestBed.configureTestingModule({
        imports: [
            HttpClientTestingModule
        ],
        providers: [
            HttpInterceptorService,
            {
                provide: HTTP_INTERCEPTORS,
                useClass: HttpInterceptorService,
                multi: true
            }
        ]
    }));

    it('should be created', () => {
        const service: HttpInterceptorService = TestBed.get(HttpInterceptorService);
        expect(service).toBeTruthy();
    });

    it('should get http 404', () => {
        const http: HttpClient = TestBed.get(HttpClient);

        http.get('/fake-route').subscribe((response: any) => {
            expect(response).toBeTruthy();
            expect(response.status).toEqual('404');
        });
    });
});

所以我正在測試是否成功獲得404,但是我不知道如何測試攔截器是否重復了兩次。

編輯

實際上,我錯了,甚至我的'should get http 404'測試都無法正常工作,只是總是給出錯誤的肯定。

編輯2

我相信我已經越來越近了,404現在可以正常工作,並且我已經為“重試”添加了一個測試,但是它仍然沒有按預期工作,我認為攔截器可能甚至沒有被調用...

it('should repeat failed request 2 more times', () => {
    const emsg = 'deliberate 404 error';

    jasmine.clock().install();
    spyOn(httpClient, 'get').and.callThrough();

    expect(httpClient.get).not.toHaveBeenCalled();

    httpClient.get(fakeUrl).subscribe(
    (response) => {
      fail('should have failed with the 404 error');
    },
    (error) => {
      expect(error.status).toEqual(404, 'status');
      expect(error.error).toEqual(emsg, 'message');
    });

    jasmine.clock().tick(3000);

    expect(httpClient.get).toHaveBeenCalledTimes(3);

    jasmine.clock().uninstall();
});

並且此測試失敗,並顯示“預期的間諜被調用了3次。被調用了1次”

好的,最后弄清楚了,我最新的方法(第2版)也不是正確的方法。 這是我重復的最終測試和工作測試:

it('should handle 404 with retry (2 times)', () => {
    const emsg = 'deliberate 404 error';

    httpClient.get(fakeUrl).subscribe(
    (response) => {
        fail('should have failed with the 404 error');
    },
    (error: HttpErrorResponse) => {
        expect(error.status).toEqual(404, 'status');
        expect(error.error).toEqual(emsg, 'message');
    });

    const retry = 2;
    for (let i = 0, c = retry + 1; i < c; i++) {
        const req = httpTestingController.expectOne(fakeUrl);
        req.flush(emsg, { status: 404, statusText: 'Not Found' });
    }
});

還添加了一個斷言,以在每次測試后運行,以確保不再有未決的請求:

afterEach(() => {
    httpTestingController.verify();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM