如何使用其他提供程序的实例在TestBed.configureTestingModule()
中实例化提供程序?
一个例子(显然没有工作):
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ServiceOne, useValue: new ServiceOne('parameterOne')},
{ provide: ServiceTwo, useValue: new ServiceTwo(TestBed.get(ServiceOne), 'parameterTwo')}
]
});
});
答案 0 :(得分:2)
providers: [
{ provide: ServiceOne, useValue: new ServiceOne('parameterOne')},
{
provide: ServiceTwo,
deps: [ ServiceOne ],
useFactory: (serviceOne: ServiceOne) => {
return new ServiceTwo(serviceOne, 'parameterTwo')
}
}
]