I am currently testing a service that has the following constructor:
constructor(@Inject('enviroment') environment) {
this.initConfig(environment);
}
The parameter environment
is provided in app.module.ts under providers like so:
{ provide: 'environment', useValue: environment }
In order to configure the TestBed
, I did the following:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{ provide: 'environment', useValue: testEnv }, TestService]
});
service = TestBed.get(TestService);
});
However, I keep encountering the error message:
Error: StaticInjectorError(DynamicTestModule)[enviroment]: StaticInjectorError(Platform: core)[enviroment]: NullInjectorError: No provider for enviroment!
The code functions correctly when serving or building, leading me to believe the issue lies in how I set up the TestBed
. I even attempted using useFactory
without success.
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{ provide: 'environment', useFactory: () => testEnv }, TestService]
});
service = TestBed.get(TestService);
});