I am trying to test retry
on a failed httpClient post request without using a for loop and I am having a hard time finding an example of doing so. Most solutions seem to show the for loop method.
Here is the service code and test in question. This currently works as expected, how can I do it the rxjs observable functional way?
Service code:
public getUsers(): Observable<UsersResponse> {
return this.httpClient.post<UsersResponse>(`${usersUrl}/fetchall`, '').pipe(
retry(3),
catchError(() => {
return of({} as UsersResponse)
}),
)
}
Test:
it('getUsers should return an empty value on error', () => {
sub = usersService
.getUsers()
.pipe(
tap((res) => {
expect(res).toEqual({} as UsersResponse)
}),
)
.subscribe()
for (let i = 0, c = retryCount + 1; i < c; i++) {
const req = httpMock.expectOne('/api/fetchall')
req.flush({}, { status: 404, statusText: 'Not Found' })
}
})