首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模拟生成的API服务

模拟生成的API服务
EN

Stack Overflow用户
提问于 2021-09-23 09:46:58
回答 1查看 179关注 0票数 1

在我们的角度应用程序中,我们使用从API生成的服务,并且在组件或使用它们的自定义服务的单元测试中面临问题。

代码语言:javascript
复制
// 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>>;

在定制服务中的使用

代码语言:javascript
复制
public getConnection(): Observable<ConnectionResultDto> {
  return this.connectionServiceAPI.getCurrentConnection('body').pipe(tap(res => this.updateConnectionStatus(res)));
}

并试图在单元测试中模拟它(我们使用Jest + Spectator + Ng-Mocks)

代码语言:javascript
复制
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代码下划线)

代码语言:javascript
复制
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运算符在未定义的结果上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-06 21:46:28

这里的问题是,类型记录聚合了所有参数和所有返回类型。因此,它的模拟也应该遵循聚合类型。

在这种情况下,(_: 'body') => of(CONNECTION_MOCK)只是3种类型中的1种。类型记录期望_'body' | 'response' | 'events',返回类型也是这样:应该尊重所有3种类型。

所有这些都导致使用as never更简单,因为很难实现所需的回调。

代码语言:javascript
复制
MockProvider(ConnectionControllerService, {
  getCurrentConnection: (_: 'body') => of(CONNECTION_MOCK),
} as never); // as never

或较弱的选择

代码语言:javascript
复制
// jasmine
MockProvider(ConnectionControllerService, {
  getCurrentConnection: jasmine
    .createSpy()
    .and.returnValue(of(CONNECTION_MOCK)),
});

// jest
MockProvider(ConnectionControllerService, {
  getCurrentConnection: jest
    .fn()
    .mockReturnValue(of(CONNECTION_MOCK)),
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69297698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档