Difference between getTestBed and TestBed

Viewed 977

Lets say i have a test config like below

 TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [SomeService]
        });

        injector = getTestBed()

If i want to get the inject service , what's the difference between the

TestBed.get(SomeService)
injector.get(SomeService)
1 Answers

That's interesting. I think this is difference between getTestBed and TestBed:

TestBed configures and initializes environment for unit testing and provides methods for creating components and services in unit tests. And getTestBed returns a singleton of the applicable TestBed.

More deeply

TestBed is the primary api for writing unit tests for Angular applications and libraries.

interface TestBed {
  platform: PlatformRef
  ngModule: Type<any> | Type<any>[]
  initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void
  resetTestEnvironment(): void
  resetTestingModule(): void
  configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
  configureTestingModule(moduleDef: TestModuleMetadata): void
  compileComponents(): Promise<any>
  get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any
  execute(tokens: any[], fn: Function, context?: any): any
  overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void
  overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void
  overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void
  overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void
  overrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void
  overrideTemplateUsingTestingModule(component: Type<any>, template: string): void
  createComponent<T>(component: Type<T>): ComponentFixture<T>
}

and then getTestBed is an instance of TestBedViewEngine or TestBedRender3.

const getTestBed: () => TestBed;
Related