Configures and initializes environment for unit testing and provides methods for creating components and services in unit tests.
class TestBed implements Injector { static initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed static resetTestEnvironment() static resetTestingModule(): typeof TestBed static configureCompiler(config: {...}): typeof TestBed static configureTestingModule(moduleDef: TestModuleMetadata): typeof TestBed static compileComponents(): Promise<any> static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed static overrideComponent(component: Type<any>, override: MetadataOverride<Component>): typeof TestBed static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): typeof TestBed static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed static overrideTemplate(component: Type<any>, template: string): typeof TestBed static overrideTemplateUsingTestingModule(component: Type<any>, template: string): typeof TestBed static overrideProvider(token: any, provider: {...}): typeof TestBed static deprecatedOverrideProvider(token: any, provider: {...}): typeof TestBed static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) static createComponent<T>(component: Type<T>): ComponentFixture<T> platform: PlatformRef ngModule: Type<any> | Type<any>[] initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]) resetTestEnvironment() resetTestingModule() configureCompiler(config: {...}) configureTestingModule(moduleDef: TestModuleMetadata) compileComponents(): Promise<any> get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) 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: {...}): void deprecatedOverrideProvider(token: any, provider: {...}): void overrideTemplateUsingTestingModule(component: Type<any>, template: string) createComponent<T>(component: Type<T>): ComponentFixture<T> }说明
TestBed is the primary api for writing unit tests for Angular applications and libraries.
静态方法
|
---|
Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite. |
static initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed参数ngModule | Type: Type | Type[] . | platform | Type: PlatformRef . | aotSummaries | Type: () => any[] . 可选. 默认值是 undefined . |
返回值TestBed
|
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'. |
|
---|
Reset the providers for the test injector. |
static resetTestEnvironment()参数没有参数。 |
|
---|
Allows overriding default compiler providers and settings which are defined in test_injector.js |
static configureCompiler(config: { providers?: any[]; useJit?: boolean; }): typeof TestBed参数config | Type: {
providers?: any[];
useJit?: boolean;
} . |
返回值typeof TestBed
|
|
---|
Allows overriding default providers, directives, pipes, modules of the test injector, which are defined in test_injector.js |
static configureTestingModule(moduleDef: TestModuleMetadata): typeof TestBed参数返回值typeof TestBed
|
|
---|
Compile components with a templateUrl for the test's NgModule. It is necessary to call this function as fetching urls is asynchronous. |
static compileComponents(): Promise<any>参数没有参数。 返回值Promise<any>
|
overrideTemplateUsingTestingModule() |
---|
Overrides the template of the given component, compiling the template in the context of the TestingModule. |
static overrideTemplateUsingTestingModule(component: Type<any>, template: string): typeof TestBed参数component | Type: Type . | template | Type: string . |
返回值typeof TestBed
|
Note: This works for JIT and AOTed components as well. |
|
---|
Overwrites all providers for the given token with the given provider definition. static overrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): typeof TestBed参数token | Type: any . | provider | Type: {
useFactory: Function;
deps: any[];
} . |
返回值typeof TestBed
|
static overrideProvider(token: any, provider: { useValue: any; }): typeof TestBed参数token | Type: any . | provider | Type: {
useValue: any;
} . |
返回值typeof TestBed
|
Note: This works for JIT and AOTed components as well. |
deprecatedOverrideProvider() |
---|
Overwrites all providers for the given token with the given provider definition. static deprecatedOverrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void参数token | Type: any . | provider | Type: {
useFactory: Function;
deps: any[];
} . |
返回值void
|
static deprecatedOverrideProvider(token: any, provider: { useValue: any; }): void参数token | Type: any . | provider | Type: {
useValue: any;
} . |
返回值void
|
方法
|
---|
Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite. |
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[])参数ngModule | Type: Type | Type[] . | platform | Type: PlatformRef . | aotSummaries | Type: () => any[] . 可选. 默认值是 undefined . |
|
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'. |
|
---|
Reset the providers for the test injector. |
resetTestEnvironment()参数没有参数。 |
|
---|
resetTestingModule()参数没有参数。 |
|
---|
configureCompiler(config: { providers?: any[]; useJit?: boolean; })参数config | Type: {
providers?: any[];
useJit?: boolean;
} . |
|
|
---|
compileComponents(): Promise<any>参数没有参数。 返回值Promise<any>
|
|
---|
execute(tokens: any[], fn: Function, context?: any): any参数tokens | Type: any[] . | fn | Type: Function . | context | Type: any . 可选. 默认值是 undefined . |
返回值any
|
|
---|
Overwrites all providers for the given token with the given provider definition. overrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void参数token | Type: any . | provider | Type: {
useFactory: Function;
deps: any[];
} . |
返回值void
|
overrideProvider(token: any, provider: { useValue: any; }): void参数token | Type: any . | provider | Type: {
useValue: any;
} . |
返回值void
|
deprecatedOverrideProvider() |
---|
Overwrites all providers for the given token with the given provider definition. deprecatedOverrideProvider(token: any, provider: { useFactory: Function; deps: any[]; }): void参数token | Type: any . | provider | Type: {
useFactory: Function;
deps: any[];
} . |
返回值void
|
deprecatedOverrideProvider(token: any, provider: { useValue: any; }): void参数token | Type: any . | provider | Type: {
useValue: any;
} . |
返回值void
|
overrideTemplateUsingTestingModule() |
---|
overrideTemplateUsingTestingModule(component: Type<any>, template: string)参数component | Type: Type . | template | Type: string . |
|