class
Configures and initializes environment for unit testing and provides methods for creating components and services in unit tests.
class 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> inject<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue?: T, flags?: InjectFlags): T 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> }
TestBed
is the primary api for writing unit tests for Angular applications and libraries.
Note: Use TestBed
in tests. It will be set to either TestBedViewEngine
or TestBedRender3
according to the compiler used.
Property | Description |
---|---|
platform: PlatformRef | |
ngModule: Type<any> | Type<any>[] |
initTestEnvironment() | |||||||||
---|---|---|---|---|---|---|---|---|---|
Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite. | |||||||||
|
ngModule | Type | |
platform | PlatformRef | |
aotSummaries | () => any[] | Optional. Default is |
void
This may only be called once, to set up the common providers for the current test suite on the current platform. If you absolutely need to change the providers, first use resetTestEnvironment
.
Test modules and platforms for individual platforms are available from '@angular/<platform_name>/testing'.
resetTestEnvironment() |
---|
Reset the providers for the test injector. |
|
resetTestingModule() |
---|
|
configureCompiler() | |||
---|---|---|---|
|
config | object |
void
configureTestingModule() | |||
---|---|---|---|
|
moduleDef | TestModuleMetadata |
void
compileComponents() |
---|
|
inject() | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
token | Type | |
notFoundValue | null | |
flags | InjectFlags | Optional. Default is |
T | null
get() | ||||||
---|---|---|---|---|---|---|
|
token | any | |
notFoundValue | any | Optional. Default is |
any
execute() | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
tokens | any[] | |
fn | Function | |
context | any | Optional. Default is |
any
overrideModule() | ||||||
---|---|---|---|---|---|---|
|
ngModule | Type | |
override | MetadataOverride |
void
overrideComponent() | ||||||
---|---|---|---|---|---|---|
|
component | Type | |
override | MetadataOverride |
void
overrideDirective() | ||||||
---|---|---|---|---|---|---|
|
directive | Type | |
override | MetadataOverride |
void
overridePipe() | ||||||
---|---|---|---|---|---|---|
|
pipe | Type | |
override | MetadataOverride |
void
overrideProvider() | ||||||
---|---|---|---|---|---|---|
Overwrites all providers for the given token with the given provider definition. | ||||||
|
token | any | |
provider | { useValue: any; } |
void
overrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): void
token | any | |
provider | object |
void
overrideTemplateUsingTestingModule() | ||||||
---|---|---|---|---|---|---|
|
component | Type | |
template | string |
void
createComponent() |
---|
© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v10.angular.io/api/core/testing/TestBed