填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

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, aotSummaries?: () => any[]): void
  resetTestEnvironment(): void
  resetTestingModule(): void
  configureCompiler(config: { providers?: any[]; useJit?: boolean; }): void
  configureTestingModule(moduleDef: TestModuleMetadata): void
  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>): 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 是用於為 Angular 應用程式和函式庫編寫單元測試的主要 API。

TestBed is the primary api for writing unit tests for Angular applications and libraries.

注意:在測試中使用 TestBed。根據使用的編譯器不同,它將被設定為 TestBedViewEngineTestBedRender3

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>[]

方法

使用編譯器工廠、PlatformRef 和 angular 模組來初始化測試環境。這些對於套件中的每個測試都是公共的。

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
    
引數
ngModule Type | Type[]
platform PlatformRef
aotSummaries () => any[]
可選. 預設值是 `undefined`.
返回值

void

這隻能呼叫一次,以在當前平臺上為當前測試套件設定公用提供者。如果你必須要更改提供者,請首先使用 resetTestEnvironment

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.

可從 '@angular/<platform_name>/testing' 獲得適用於各個平臺的測試模組和平臺。

Test modules and platforms for individual platforms are available from '@angular/<platform_name>/testing'.

重置測試注入器的提供者。

Reset the providers for the test injector.

      
      resetTestEnvironment(): void
    
引數

沒有引數。

返回值

void

      
      resetTestingModule(): void
    
引數

沒有引數。

返回值

void

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

void

      
      configureTestingModule(moduleDef: TestModuleMetadata): void
    
引數
moduleDef TestModuleMetadata
返回值

void

      
      compileComponents(): Promise<any>
    
引數

沒有引數。

返回值

Promise<any>

      
      inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null
    
引數
token ProviderToken
notFoundValue null
flags InjectFlags
可選. 預設值是 `undefined`.
返回值

T | null

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

從 v9.0.0 開始使用 TestBed.inject

Deprecated from v9.0.0 use TestBed.inject

引數
token any
notFoundValue any
可選. 預設值是 `undefined`.
返回值

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
    
引數
ngModule Type
override MetadataOverride
返回值

void

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

void

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

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
    
引數
token any
provider { useValue: 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
    
引數
component Type
template string
返回值

void

      
      createComponent<T>(component: Type<T>): ComponentFixture<T>
    
引數
component Type
返回值

ComponentFixture<T>