我试图在TypeScript中监视一个通用方法,但无法使Jasmine识别它。
我的代码如下:
http: HttpClient <- Not proper code, just showing type.
...
this.http.get<Customer[]>(url);
我想模拟get<...>
方法。
const httpMock = {} as HttpClient;
spyOn(httpMock, 'get')
.and.returnValue(of({} as Customer[]));
但是当我运行测试时,会出现以下错误:
错误:get()方法不存在
用法:spyOn(,)
const httpMock = { get: () => null } as HttpClient;
,但是在测试HTTP时应该使用HttpTestingController
(https://angular.io/api/common/http/testing/HttpTestingController)。 - AliF50HttpTestingController
难道不能解决确保调用已经被执行的问题吗?它不是模拟返回结果的吗? - LosManos