I know that this question has been asked over and over. I was not able to find a solution which works. I'm currently adding unit tests to our project and thus fixing all auto generated ones.
But when running ng test, I get the following error:
NullInjectorError: R3InjectorError(DynamicTestModule)[Service -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
Notice the -> HttpClient -> HttpClient.
When I first saw this, I thought it must be a circular dependency problem. Which led me to create a testing Testing Module, which i'm importing into my TestBed.
Here is a sample Test which fails.
import { TestBed } from '@angular/core/testing';
import { SearchListService } from './search-list.service';
import { ServiceTestingModule } from '@app/testing/service.testing.module';
import { ZhwKnowledgeJourneyService } from '@bkh/services/zhw-knowledge-journey.service';
describe('ZhwKnowledgeJourneyService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [ServiceTestingModule],
providers: [SearchListService]
}));
it('should be created', () => {
const service: ZhwKnowledgeJourneyService = TestBed.inject(ZhwKnowledgeJourneyService);
expect(service).toBeTruthy();
});
});
And here is my "ServiceTestingModule"
import { NgModule } from '@angular/core';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
CommonModule,
HttpClientModule,
HttpClientTestingModule,
],
declarations: [],
providers: [],
exports: [HttpClientModule, HttpClientTestingModule]
})
export class ServiceTestingModule { }
I have also checked, that "imports" is always before "providers", but still no luck.
And i read also (and tested) all Github and Stackoverflow posts to this topic but since i had no luck there, i'm asking this question again.
Thanks in advance