2

我正在尝试设置一个集成测试,它将使用 ngrx/data 实体从后端 API 服务中获取一些数据。

我设置了这个 StackBlitz:https ://stackblitz.com/edit/ngrxdata-testing-not-working-pxfmkb?file=src/main.ts

它应该在启动时运行测试 - 我的测试用例中没有任何期望,但是我正在查看控制台日志并期望它显示登录ClientDataService/src/app/data/client/client-data.service.ts),即:

console.log('never goes here :(');

在集成测试(data.integration.spec.ts)中,我正在配置模块,定义客户端实体类型并包括AppDataServiceModule依次执行此操作:

import { NgModule } from '@angular/core';
import { ClientDataService } from './client/client-data.service';
import { EntityDataService, EntityDefinitionService } from '@ngrx/data';

@NgModule({
  providers: [
    ClientDataService,
  ],
})
export class AppDataServiceModule {
  constructor(
    entityDataService: EntityDataService,
    clientDataService: ClientDataService
  ) {
    entityDataService.registerService('Client', clientDataService);
  }
}

如您所见,我正在按照ngrx docs的建议注册数据服务,在这里

我觉得我已经很接近了,但只需要朝着正确的方向轻推就可以让它发挥作用!

4

1 回答 1

1

自定义 DataService 必须扩展DefaultDataService. 应该看起来像这样:

export class ClientDataService extends DefaultDataService<Client> {

  constructor(
    http: HttpClient, httpUrlGenerator: HttpUrlGenerator
  ) {
    super('Client', http, httpUrlGenerator);
  }

  public getAll(): Observable<any> {
    // get Data here
  }
}

BackendService必须返回 Observable :

public getClients(): Observable<Array<Client>> {
  // will be mocked
  return of([
    {
      id: '1: Will not return as it will be mocked'
    },
    {
      id: '2: Will not return as it will be mocked'
    }
  ])
}

还有两件事对我来说看起来很可疑:

  1. 代码中没有订阅,所以我认为你Observable很冷。
  2. clientResolve.resolve({}, {})调用需要一个ActivatedRouteSnapshot作为第一个参数。我对Resolve界面不太熟悉,但也许这也是一个问题。
于 2020-04-04T23:28:16.847 回答