如何用另一个提供程序的实例实例化TestBed.configureTestingModule()
中的提供程序?
一个例子(显然不起作用):
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ServiceOne, useValue: new ServiceOne('parameterOne')},
{ provide: ServiceTwo, useValue: new ServiceTwo(TestBed.get(ServiceOne), 'parameterTwo')}
]
});
});
最佳答案
providers: [
{ provide: ServiceOne, useValue: new ServiceOne('parameterOne')},
{
provide: ServiceTwo,
deps: [ ServiceOne ],
useFactory: (serviceOne: ServiceOne) => {
return new ServiceTwo(serviceOne, 'parameterTwo')
}
}
]