填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

TestBed

Configures and initializes environment for unit testing and provides methods for creating components and services in unit tests.

查看"说明"...

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

属性

属性说明
platform: PlatformRef
ngModule: Type<any> | Type<any>[]

方法

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[]): void
      
      initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void
    
参数
ngModule Type | Type[]
platform PlatformRef
aotSummaries () => any[]

可选. 默认值是 undefined.

返回值

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'.

Reset the providers for the test injector.

resetTestEnvironment(): void
      
      resetTestEnvironment(): void
    
参数

没有参数。

返回值

void

resetTestingModule(): void
      
      resetTestingModule(): void
    
参数

没有参数。

返回值

void

configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
      
      configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
    
参数
config object
返回值

void

configureTestingModule(moduleDef: TestModuleMetadata): void
      
      configureTestingModule(moduleDef: TestModuleMetadata): void
    
参数
moduleDef TestModuleMetadata
返回值

void

compileComponents(): Promise<any>
      
      compileComponents(): Promise<any>
    
参数

没有参数。

返回值

Promise<any>

inject<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue: null, flags?: InjectFlags): T | null
      
      inject<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue: null, flags?: InjectFlags): T | null
    
参数
token Type | InjectionToken | AbstractType
notFoundValue null
flags InjectFlags

可选. 默认值是 undefined.

返回值

T | null

get(token: any, notFoundValue?: any): any
      
      get(token: any, notFoundValue?: any): any
    

Deprecated from v9.0.0 use TestBed.inject

参数
token any
notFoundValue any

可选. 默认值是 undefined.

返回值

any

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

可选. 默认值是 undefined.

返回值

any

overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void
      
      overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void
    
参数
ngModule Type
override MetadataOverride
返回值

void

overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void
      
      overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void
    
参数
component Type
override MetadataOverride
返回值

void

overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void
      
      overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void
    
参数
directive Type
override MetadataOverride
返回值

void

overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void
      
      overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void
    
参数
pipe Type
override MetadataOverride
返回值

void

Overwrites all providers for the given token with the given provider definition.

overrideProvider(token: any, provider: { useValue: any; }): void
      
      overrideProvider(token: any, provider: { useValue: any; }): void
    
参数
token any
provider { useValue: any; }
返回值

void

overrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): void
      
      overrideProvider(token: any, provider: { useFactory?: Function; useValue?: any; deps?: any[]; }): void
    
参数
token any
provider object
返回值

void

overrideTemplateUsingTestingModule(component: Type<any>, template: string): void
      
      overrideTemplateUsingTestingModule(component: Type<any>, template: string): void
    
参数
component Type
template string
返回值

void

createComponent<T>(component: Type<T>): ComponentFixture<T>
      
      createComponent<T>(component: Type<T>): ComponentFixture<T>
    
参数
component Type
返回值

ComponentFixture<T>