如何用另一个提供程序的实例实例化TestBed.configureTestingModule()中的提供程序?

一个例子(显然不起作用):


beforeEach(() => {
  TestBed.configureTestingModule({
    providers: [
      { provide: ServiceOne, useValue: new ServiceOne('parameterOne')},
      { provide: ServiceTwo, useValue: new ServiceTwo(TestBed.get(ServiceOne), 'parameterTwo')}
    ]
  });
});

最佳答案

使用factory provider

providers: [
  { provide: ServiceOne, useValue: new ServiceOne('parameterOne')},
  {
    provide: ServiceTwo,
    deps: [ ServiceOne ],
    useFactory: (serviceOne: ServiceOne) => {
      return new ServiceTwo(serviceOne, 'parameterTwo')
    }
  }
]

10-08 07:01