我正在尝试测试一个修改来自 HTTP 请求的响应的拦截器。
这是我当前的代码:
@Injectable()
export class ResponseCamelCaseInterceptor implements HttpInterceptor {
intercept(
httpRequest: HttpRequest<Record<string, unknown>>,
httpHandler: HttpHandler,
): Observable<HttpEvent<Record<string, unknown>>> {
return httpHandler.handle(httpRequest).pipe(
filter(
(value): value is HttpResponse<Record<string, unknown>> =>
value instanceof HttpResponse,
),
filter(({ body }) => isPlainObject(body)),
map(httpEvent =>
httpEvent.clone({ body: snakeToCamelCase(httpEvent.body) }),
),
);
}
}
及其对应的测试文件我到目前为止:
describe(ResponseCamelCaseInterceptor.name, () => {
const createService = createServiceFactory(ResponseCamelCaseInterceptor);
test('some description', () => {
const { service } = createService();
const fakeHttpRequest = new HttpRequest('POST', 'https://...', { custom_key: '1' });
service.intercept(fakeHttpRequest, 'what should I put here for HttpHandler?').subscribe(() => {
// expect(httpResponse.body).toStrictEqual({ customKey: '1' });
});
});
});
请注意,我使用的是 Angular 10.xy、Jest 26.xy 和 Spectator 5.xy