0
Ich habe einen Dienst, der eine HTTP-Anforderung erhalten macht wieAngular 5 Httpclient HttpTestingController Testfehlerpfad
folgtpublic get(uri: string, params?: HttpParams): Observable<Object> {
return params ? this._http.get(uri, { params: params }) : this._http.get(uri)
.catch((error: HttpErrorResponse) => this.catchHttpError(error));
}
private catchHttpError(error: HttpErrorResponse) : Observable<Object> {
this._logger.error(`error occurred: ${error.message}`);
throw(error);
}
ich einen Test-Spezifikation, die den erfolgreichen Weg testen. Wie kann ich:
die HttpTestingController Get so Fehler und Irrtum, dass die catchHttpError Funktion aufgerufen wird?
Test, dass _httpService.get einen Fehler von der catchHttpError-Funktion zurückwirft?
Mein Test so sieht weit wie:
it('should make a get request and catch error', async(inject([HttpService, HttpTestingController], (_httpService: HttpService, backend: HttpTestingController) => {
spyOn(_httpService, 'get').and.callThrough();
let actualObjects: Object[] = [];
_httpService.get('/api/dummy/get/uri').subscribe((objects: Object[]) => {
actualObjects = objects;
});
backend.expectOne('/api/dummy/get/uri').flush(expectedObjects);
expect(_httpService.get).toHaveBeenCalledTimes(1);
expect(_httpService.get).toHaveBeenCalledWith('/api/dummy/get/uri');
expect(actualObjects).toEqual(expectedObjects);
expect(actualObjects.length).toBe(1);
//expect(_httpService.get).toThrow();
})));