如何在@ngrx/data 中对 EntityCollectionServiceBase 进行单元测试?如何测试@ngrx/data 服务?

时间:2021-05-08 11:29:25

标签: angular ngrx ngrx-store angular-test angular-ngrx-data

我想测试我的 ngrx 数据服务:

@Injectable({
  providedIn: 'root',
})
export class PostsService extends EntityCollectionServiceBase<Post>{
  constructor(serviceElemtsFactory: EntityCollectionServiceElementsFactory) {
    super('Post', serviceElemtsFactory);
  }
}

到目前为止,我已经定义了以下测试类:

describe('PatientService', () => {
  let service: PostsService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        EffectsModule.forRoot([]),
        StoreModule.forRoot({}),
        EntityDataModule.forRoot(entityConfig)
      ],
      providers: [
        {
          provide: EntityCollectionServiceElementsFactory,
          useValue: MockService(EntityCollectionServiceElementsFactory),
        },
        PostsService,
      ],
    }).compileComponents();
    service = TestBed.inject(PostsService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

});

但我收到错误:

TypeError: 无法解构 'serviceElementsFactory.create(...)' 的属性 'dispatcher',因为它未定义。

Karma Result: TypeError: Cannot destructure property 'dispatcher' of 'serviceElementsFactory.create(...)' as it is undefined.

如何正确修复我的测试?

0 个答案:

没有答案