【发布时间】:2020-05-08 16:19:23
【问题描述】:
我正在调用 Nestjs 服务中的 API,如下所示,
import { HttpService, Post } from '@nestjs/common';
export class MyService {
constructor(private httpClient: HttpService) {}
public myMethod(input: any) {
return this.httpClient
.post<any>(
this.someUrl,
this.createObject(input.code),
{ headers: this.createHeader() },
)
.pipe(map(response => response.data));
}
}
我如何模拟/监视对 this.httpClient.post() 的调用以在不触及实际 API 的情况下返回响应?
describe('myMethod', () => {
it('should return the value', async () => {
const input = {
code: 'value',
};
const result = ['test'];
// spyOn?
expect(await myService.myMethod(input)).toBe(result);
});
});
【问题讨论】: