On this page
TestBed
class final
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?: T, flags?: InjectFlags): T
get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any
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[]; }): TestBed
overrideTemplateUsingTestingModule(component: Type<any>, template: string): TestBed
createComponent<T>(component: Type<T>): ComponentFixture<T>
}
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. |
|||||||||
|
ngModule |
Type<any> | Type<any>[] |
|
platform |
PlatformRef |
|
options |
TestEnvironmentOptions |
Optional. Default is |
Returns
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 |
Returns
void
|
configureTestingModule()
|
|||
|---|---|---|---|
|
moduleDef |
TestModuleMetadata |
Returns
|
compileComponents()
|
|---|
|
|
inject()
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|
|
token |
ProviderToken<T> |
|
notFoundValue |
T |
Optional. Default is |
flags |
InjectFlags |
Optional. Default is |
Returns
T
inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null
Parameters
token |
ProviderToken<T> |
|
notFoundValue |
null |
|
flags |
InjectFlags |
Optional. Default is |
Returns
T | null
|
get()
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|
|
token |
ProviderToken<T> |
|
notFoundValue |
T |
Optional. Default is |
flags |
InjectFlags |
Optional. Default is |
Returns
any
get(token: any, notFoundValue?: any): any
Deprecated from v9.0.0 use TestBed.inject
Parameters
token |
any |
|
notFoundValue |
any |
Optional. Default is |
Returns
any
|
execute()
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|
|
tokens |
any[] |
|
fn |
Function |
|
context |
any |
Optional. Default is |
Returns
any
|
overrideModule()
|
||||||
|---|---|---|---|---|---|---|
|
ngModule |
Type<any> |
|
override |
MetadataOverride<NgModule> |
Returns
|
overrideComponent()
|
||||||
|---|---|---|---|---|---|---|
|
component |
Type<any> |
|
override |
MetadataOverride<Component> |
Returns
|
overrideDirective()
|
||||||
|---|---|---|---|---|---|---|
|
directive |
Type<any> |
|
override |
MetadataOverride<Directive> |
Returns
|
overridePipe()
|
||||||
|---|---|---|---|---|---|---|
|
pipe |
Type<any> |
|
override |
MetadataOverride<Pipe> |
Returns
|
overrideTemplate()
|
|---|
|
overrideProvider()
|
||||||
|---|---|---|---|---|---|---|
Overwrites all providers for the given token with the given provider definition. |
||||||
|
token |
any |
|
provider |
object |
Returns
overrideProvider(token: any, provider: { useValue: any; }): TestBed
Parameters
token |
any |
|
provider |
{ useValue: any; } |
Returns
overrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): TestBed
Parameters
token |
any |
|
provider |
object |
Returns
|
overrideTemplateUsingTestingModule()
|
|---|
|
createComponent()
|
|---|
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v14.angular.io/api/core/testing/TestBed