TestBed

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()

参数

没有参数。

static resetTestingModule(): typeof TestBed

参数

没有参数。

返回值

typeof TestBed

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

参数

moduleDef

Type: TestModuleMetadata.

返回值

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>

static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed

参数

ngModule

Type: Type.

override

Type: MetadataOverride.

返回值

typeof TestBed

static overrideComponent(component: Type<any>, override: MetadataOverride<Component>): typeof TestBed

参数

component

Type: Type.

override

Type: MetadataOverride.

返回值

typeof TestBed

static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): typeof TestBed

参数

directive

Type: Type.

override

Type: MetadataOverride.

返回值

typeof TestBed

static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed

参数

pipe

Type: Type.

override

Type: MetadataOverride.

返回值

typeof TestBed

static overrideTemplate(component: Type<any>, template: string): typeof TestBed

参数

component

Type: Type.

template

Type: string.

返回值

typeof TestBed

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.

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

static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND)

参数

token

Type: any.

notFoundValue

Type: any.

可选. 默认值是 Injector.THROW_IF_NOT_FOUND.

static createComponent<T>(component: Type<T>): ComponentFixture<T>

参数

component

Type: Type.

返回值

ComponentFixture<T>

属性

属性名类型说明
platform
ngModule

方法

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; }.

configureTestingModule(moduleDef: TestModuleMetadata)

参数

moduleDef

Type: TestModuleMetadata.

compileComponents(): Promise<any>

参数

没有参数。

返回值

Promise<any>

get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND)

参数

token

Type: any.

notFoundValue

Type: any.

可选. 默认值是 Injector.THROW_IF_NOT_FOUND.

execute(tokens: any[], fn: Function, context?: any): any

参数

tokens

Type: any[].

fn

Type: Function.

context

Type: any.

可选. 默认值是 undefined.

返回值

any

overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void

参数

ngModule

Type: Type.

override

Type: MetadataOverride.

返回值

void

overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void

参数

component

Type: Type.

override

Type: MetadataOverride.

返回值

void

overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void

参数

directive

Type: Type.

override

Type: MetadataOverride.

返回值

void

overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void

参数

pipe

Type: Type.

override

Type: MetadataOverride.

返回值

void

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

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(component: Type<any>, template: string)

参数

component

Type: Type.

template

Type: string.

createComponent<T>(component: Type<T>): ComponentFixture<T>

参数

component

Type: Type.

返回值

ComponentFixture<T>