I m trying to test a component where I m injecting a specific service declared in the shared module as below.
providers: [
{
provide: 'ParameterSelectionService',
useFactory: () =>
SharedModule.getSelectionServiceInstance<ParameterModel>(
'ParameterModel'
)
},
{
provide: 'RuleSelectionService',
useFactory: () =>
SharedModule.getSelectionServiceInstance<RuleModel>('RuleModel')
}
]
})
export class SharedModule {
static instances: Map<string, SelectionService<any>> = new Map();
static getSelectionServiceInstance<T>(keyName: string): T {
let instance = this.instances.get(keyName);
if (instance === undefined) {
instance = new SelectionService<T>();
this.instances[keyName] = instance;
}
return instance as unknown as T;
}
}
In the component I m using the @Inject to specify the provider
constructor(
@Inject('RuleSelectionService')
private ruleSelectionService: SelectionService<RuleModel>,
@Inject('ParameterSelectionService')
private parameterSelectionService: SelectionService<ParameterModel>, ... )
while writting my test I had two instances of the Rules Selection Service so I tried googling how to specify in test what provider to use to inject but didn't found anything ( my search was not giving results I feel like missing keywords ) so I tryied to reverse engeneer the Inject to understand and debug it working, this leaded me to this attempt
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [DiliParametersCategoryComponent],
imports: [
CoreModule,
MaterialModule,
RouterTestingModule,
SharedModule,
TranslateModule.forRoot()
]
}).compileComponents();
});
beforeEach(() => {
const paramProviderToken : InjectionToken<SelectionService<ParameterModel>> = new InjectionToken<SelectionService<ParameterModel>>('ParameterSelectionService');
parameterSelectionService = TestBed.inject<SelectionService<ParameterModel>>(paramProviderToken);
const ruleServiceProviderToken : InjectionToken<SelectionService<RuleModel>> = new InjectionToken<SelectionService<RuleModel>>('RuleSelectionService');
ruleSelectionService = TestBed.inject<SelectionService<RuleModel>>(ruleServiceProviderToken);
});
However my injection now is undefined !!!
What should I do to get the desired service injection on testing using the configuration that I already made on the shared module.