I need to write unit tests for the following DataService
,
@Injectable()
export class DataService {
constructor(private config: ConfigService, private http: HttpClient) {
}
.....
someMethod(){
let apiUrl = this.config.get('api').url; // LINE 1
}
}
The ConfigService
is injected to DataService
that has a load
function which gets the configuration from a json file. This load
function will be invoked when the app is initialized.
export function configServiceFactory(config: ConfigService) {
return () => config.load();
}
...
providers: [
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: configServiceFactory,
deps: [ConfigService],
multi: true
}
]
Here's a part of data-service.spect.ts
file,
...
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [DataService, ConfigService]
});
mock = TestBed.get(HttpTestingController);
service = TestBed.get(DataService);
});
....
so when i run the test, in LINE 1
i get that the this.config.get('api')
is undefined. I can understand that it is because the ConfigService
did not load the data from JSON. So now how i can i make the injected services also make the async calls during unit tests?