TestBed
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, options?: TestEnvironmentOptions): void
resetTestEnvironment(): void
resetTestingModule(): TestBed
configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
configureTestingModule(moduleDef: TestModuleMetadata): TestBed
compileComponents(): Promise<any>
inject<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & { optional?: false; }): T
inject<T>(token: ProviderToken<T>, notFoundValue: null, options: InjectOptions): T | null
inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T
inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T
inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null
get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any
get(token: any, notFoundValue?: any): any
runInInjectionContext<T>(fn: () => T): T
execute(tokens: any[], fn: Function, context?: any): any
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBed
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBed
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBed
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): TestBed
overrideTemplate(component: Type<any>, template: string): TestBed
overrideProvider(token: any, provider: { useFactory: Function; deps: any[]; multi?: boolean; }): TestBed
overrideProvider(token: any, provider: { useValue: any; multi?: boolean; }): TestBed
overrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; multi?: boolean; }): TestBed
overrideTemplateUsingTestingModule(component: Type<any>, template: string): TestBed
createComponent<T>(component: Type<T>): ComponentFixture<T>
flushEffects(): void
}
Description
TestBed
is the primary api for writing unit tests for Angular applications and libraries.
Properties
Property | Description |
---|---|
platform: PlatformRef
|
Read-Only |
ngModule: Type<any> | Type<any>[]
|
Read-Only |
Methods
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. |
|||||||||
Parameters
Returns
|
|||||||||
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 Test modules and platforms for individual platforms are available from '@angular/<platform_name>/testing'. |
resetTestEnvironment() |
---|
Reset the providers for the test injector. |
ParametersThere are no parameters. Returns
|
resetTestingModule() |
---|
configureCompiler() |
---|
configureTestingModule() | |||
---|---|---|---|
Parameters
Returns |
compileComponents() |
---|
ParametersThere are no parameters. Returns
|
inject() | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
token
|
ProviderToken<T> |
|
notFoundValue
|
null |
|
options
|
InjectOptions |
Returns
T | null
Overload #2
inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T
Parameters
token
|
ProviderToken<T> |
|
notFoundValue
|
T |
Optional. Default is |
options
|
InjectOptions |
Optional. Default is |
Returns
T
Overload #3
inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T
Deprecated use object-based flags (InjectOptions
) instead.
Parameters
token
|
ProviderToken<T> |
|
notFoundValue
|
T |
Optional. Default is |
flags
|
InjectFlags |
Optional. Default is |
Returns
T
Overload #4
inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null
Deprecated use object-based flags (InjectOptions
) instead.
Parameters
token
|
ProviderToken<T> |
|
notFoundValue
|
null |
|
flags
|
InjectFlags |
Optional. Default is |
Returns
T | null
get() | |||||||||
---|---|---|---|---|---|---|---|---|---|
Deprecated from v9.0.0 use TestBed.inject Parameters
Returns
|
|||||||||
runInInjectionContext() |
---|
Runs the given function in the See also: |
execute() |
---|
overrideModule() | ||||||
---|---|---|---|---|---|---|
Parameters
Returns |
overrideComponent() | ||||||
---|---|---|---|---|---|---|
Parameters
Returns |
overrideDirective() | ||||||
---|---|---|---|---|---|---|
Parameters
Returns |
overridePipe() | ||||||
---|---|---|---|---|---|---|
Parameters
Returns |
overrideTemplate() |
---|
overrideProvider() |
---|
Overwrites all providers for the given token with the given provider definition. |
overrideTemplateUsingTestingModule() |
---|
createComponent() |
---|
flushEffects() |
---|
Execute any pending effects. |
ParametersThere are no parameters. Returns
|