- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试测试修改来自 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' });
});
});
});
最佳答案
我能够获得截取方法来执行以下操作。根据需要修改 mockHandler.handle 返回值。
const mockHandler = {
handle: jest.fn(() => of(new HttpResponse({status: 200, body: {data: 'thisIsWhatImTesting'}})))
};
spectator.service.intercept(new HttpRequest<any>(HttpMethod.GET, '/api'), mockHandler)
.subscribe((response: HttpResponse<any>) => {
expect(response.body).toStrictEqual({customKey: '1'});
});
在 subscribe lambda 中,您需要将响应指定为输入。这应该是拦截器处理后的 HttpResponse 。
关于angular - 使用 Jest 和 Spectator 测试 Angular 拦截器的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63653206/
本文整理了Java中com.netflix.spectator.impl.matcher.ZeroOrMoreMatcher类的一些代码示例,展示了ZeroOrMoreMatcher类的具体用法。这些
我正在尝试与 Angular、Jest 和 Spectator 一起编写单元测试。现在我正在努力模拟 NgRx 商店。尝试从 Spectator 实例获取 NgRx 提供的 MockStore 实例时
您能否为 Spectator View 装备使用 USB 摄像头并覆盖 OpenCV 用于获取摄像头馈送的脚本之一? 我认为这是第一个 StackOverflow 问题,它讨论了 Microsoft
本文整理了Java中com.netflix.spectator.impl.matcher.ZeroOrMoreMatcher.next()方法的一些代码示例,展示了ZeroOrMoreMatcher.
本文整理了Java中com.netflix.spectator.impl.matcher.ZeroOrMoreMatcher.repeated()方法的一些代码示例,展示了ZeroOrMoreMatc
本文整理了Java中com.netflix.spectator.impl.matcher.ZeroOrMoreMatcher.()方法的一些代码示例,展示了ZeroOrMoreMatcher.()的具
我正在尝试测试修改来自 HTTP 请求的响应的拦截器。 这是我当前的代码: @Injectable() export class ResponseCamelCaseInterceptor implem
我正在我的本地机器上玩所有这些很棒的 Spring Cloud 东西。我有一个基于 Zuul 的 API 网关发现和调用 Spring Data Rest 服务。效果很好。 下面介绍一下Spectat
我正在尝试使用 Spectator用于计数。用法如下: @Autowired private Registry registry; 然后调用: registry.counter("ping").cou
我的组件订阅了一个服务中的 Observable,它通过一个 Ngrx 选择器填充,为了简洁起见,这里概括为: export class MyService { signInFalied$: Ob
我想用spectator帮助我进行单元测试,因为我已经阅读了很多关于它的东西。但是,我不确定如何解决我的测试问题。在我的组件模板中,我有一个 formGroup像这样... 在我的 TS 文件中
我是一名优秀的程序员,十分优秀!