I have a service which update a private field
export class ConfigService {
private config: any;
init() {
return this.http.get('url').toPromise()
.then(configResp =>
this.config = configResp;
return configResp;
)
);
}
get() {
return this.config;
}
}
app.component.ts:
export class AppComponents implements OnInit {
constructor (
private configService: ConfigService
) { }
ngOnInit(): void {
const apiUrl = this.configService.get().apiUrl || '';
}
}
The config service init() method is called before boostraping the application inside of app.module.ts. I use this configuration object instead of environment variables.
I can't do code coverage 100% due of that get() function, which I use only in mocks. Because if I'm trying to test it inside the service, the config will be undefined everytime, even if I mock the init function, which maybe should update the configuration object.
Also, if I'm trying to test it inside the app.component, the result will be undefined and will throw an error when inside of ngOnInit is trying to get apiUrl from the this.configService.get() response. I can just mock it. But on code coverage, the mocks are not counted. I can do a dumb assert to check if the method is called and that should be enough maybe?
it('should call get method', () => {
service.get();
expect(service.get).toBeTruthy();
});
thanks