【发布时间】:2020-10-08 08:45:29
【问题描述】:
我尝试对 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;,但您应该使用HttpTestingController(angular.io/api/common/http/testing/HttpTestingController) 来测试HTTP。 -
HttpTestingController不解决确保电话已拨打的问题吗? - 不是在嘲笑它的答案吗? -
您还可以使用 flush 方法模拟响应。 medium.com/better-programming/… 看看。
标签: typescript generics testing jasmine spyon