0

The service I want to test has the following constructor:

constructor(@Inject('enviroment') environment) {
    this.initConfig(environment);
}

The environment is provided in app.module under providers by:

{ provide: 'environment', useValue: environment }

So I configured the TestBed as follows:

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [{ provide: 'environment', useValue: testEnv }, TestService]
    });
    service = TestBed.get(TestService);
});

And I keep getting:

Error: StaticInjectorError(DynamicTestModule)[enviroment]: StaticInjectorError(Platform: core)[enviroment]: NullInjectorError: No provider for enviroment!

The code itself works fine when serving/building so I assume the mistake is somewhere on how I configured the TestBed? I've also tried useFactory to no avail.

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [{ provide: 'environment', useFactory: ()=>testEnv }, TestService]
    });
    service = TestBed.get(TestService);
});
Yuri
  • 4,254
  • 1
  • 29
  • 46
Han Che
  • 8,239
  • 19
  • 70
  • 116
  • I would like to help with this as I have done this kind of test situation many times, but I'd need to see more of the code. Can you provide the complete spec file and the complete component file? Of course you can remove stuff if completely unrelated, but at least need to see the structure of the file. – ccamac Jun 10 '19 at 14:54
  • nvm I think I found your issue – ccamac Jun 10 '19 at 14:54
  • Thanks so much :D – Han Che Jun 10 '19 at 16:59

1 Answers1

2

I think it is just a typo.

It was missing the n in environment

constructor(@Inject('enviroment') environment) {
Yuri
  • 4,254
  • 1
  • 29
  • 46
ccamac
  • 496
  • 2
  • 8