在我们的角度应用程序中,我们使用从API生成的服务,并且在组件或使用它们的自定义服务的单元测试中面临问题。
// Generated code
getCurrentConnection(observe?: 'body', reportProgress?: boolean, options?: {
httpHeaderAccept?: '*/*';
}): Observable<ConnectionResultDto>;
getCurrentConnection(observe?: 'response', reportProgress?: boolean, options?: {
httpHeaderAccept?: '*/*';
}): Observable<HttpResponse<ConnectionResultDto>>;
getCurrentConnection(observe?: 'events', reportProgress?: boolean, options?: {
httpHeaderAccept?: '*/*';
}): Observable<HttpEvent<ConnectionResultDto>>;在定制服务中的使用
public getConnection(): Observable<ConnectionResultDto> {
return this.connectionServiceAPI.getCurrentConnection('body').pipe(tap(res => this.updateConnectionStatus(res)));
}并试图在单元测试中模拟它(我们使用Jest + Spectator + Ng-Mocks)
describe('ConnectionService', () => {
let spectator: SpectatorHttp<ConnectionService>;
const createHttp = createHttpFactory({
service: ConnectionService,
providers: [
MockProvider(ConnectionControllerService, {
getCurrentConnection: ('body') => of(CONNECTION_MOCK) // this throws error displayed below
}),
],
});这是我们得到的错误(VS代码下划线)
No overload matches this call.
Overload 1 of 3, '(instance: AnyType<ConnectionControllerService>, overrides?: Partial<ConnectionControllerService>): FactoryProvider', gave the following error.
Type 'string' is not assignable to type '{ (observe?: "body", reportProgress?: boolean, options?: { httpHeaderAccept?: "*/*"; }): Observable<ConnectionResultDto>; (observe?: "response", reportProgress?: boolean, options?: { ...; }): Observable<...>; (observe?: "events", reportProgress?: boolean, options?: { ...; }): Observable<...>; }'.
Overload 2 of 3, '(provider: string | InjectionToken<{ getCurrentConnection: string; }>, useValue?: Partial<{ getCurrentConnection: string; }>): FactoryProvider', gave the following error.
Argument of type 'typeof ConnectionControllerService' is not assignable to parameter of type 'string | InjectionToken<{ getCurrentConnection: string; }>'.
Type 'typeof ConnectionControllerService' is missing the following properties from type 'InjectionToken<{ getCurrentConnection: string; }>': _desc, ɵprov
Overload 3 of 3, '(provider: string, useValue?: Partial<{ getCurrentConnection: string; }>): FactoryProvider', gave the following error.
Argument of type 'typeof ConnectionControllerService' is not assignable to parameter of type 'string'.ts(2769)
connectionController.service.d.ts(45, 5): The expected type comes from property 'getCurrentConnection' which is declared here on type 'Partial<ConnectionControllerService>'
(method) getCurrentConnection?(observe?: "body", reportProgress?: boolean, options?: {
httpHeaderAccept?: "*/*";
}): Observable<ConnectionResultDto> (+2 overloads)你知道我们怎么才能正确地嘲笑它吗?我尝试指定getCurrentConnection方法的每个可选参数,但没有效果。在没有覆盖方法的情况下进行模拟将导致单元测试失败,因为pipe运算符在未定义的结果上。
发布于 2022-03-06 21:46:28
这里的问题是,类型记录聚合了所有参数和所有返回类型。因此,它的模拟也应该遵循聚合类型。
在这种情况下,(_: 'body') => of(CONNECTION_MOCK)只是3种类型中的1种。类型记录期望_是'body' | 'response' | 'events',返回类型也是这样:应该尊重所有3种类型。
所有这些都导致使用as never更简单,因为很难实现所需的回调。
MockProvider(ConnectionControllerService, {
getCurrentConnection: (_: 'body') => of(CONNECTION_MOCK),
} as never); // as never或较弱的选择
// jasmine
MockProvider(ConnectionControllerService, {
getCurrentConnection: jasmine
.createSpy()
.and.returnValue(of(CONNECTION_MOCK)),
});
// jest
MockProvider(ConnectionControllerService, {
getCurrentConnection: jest
.fn()
.mockReturnValue(of(CONNECTION_MOCK)),
});https://stackoverflow.com/questions/69297698
复制相似问题