W3cubDocs

/Angular 2

TestBed

Stable Class

What it does

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

Class Overview

class TestBed implements Injector {
  static initTestEnvironment(ngModule: Type<any>, platform: PlatformRef) : TestBed
  static resetTestEnvironment()
  static resetTestingModule() : typeof TestBed
  static configureCompiler(config: {providers?: any[]; useJit?: boolean;}) : 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 get(token: any, notFoundValue?: any)
  static createComponent(component: Type<T>) : ComponentFixture<T>
  initTestEnvironment(ngModule: Type<any>, platform: PlatformRef)
  resetTestEnvironment()
  resetTestingModule()
  platform : PlatformRef
  ngModule : Type<any>
  configureCompiler(config: {providers?: any[], useJit?: boolean})
  configureTestingModule(moduleDef: TestModuleMetadata)
  compileComponents() : Promise<any>
  get(token: any, notFoundValue?: 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
  createComponent(component: Type<T>) : ComponentFixture<T>
}

Class Description

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

Static Members

initTestEnvironment(ngModule: Type<any>, platform: PlatformRef) : TestBed

Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite.

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

resetTestEnvironment()

Reset the providers for the test injector.

resetTestingModule() : typeof TestBed
configureCompiler(config: {providers?: any[]; useJit?: boolean;}) : typeof TestBed

Allows overriding default compiler providers and settings which are defined in test_injector.js

configureTestingModule(moduleDef: TestModuleMetadata) : typeof TestBed

Allows overriding default providers, directives, pipes, modules of the test injector, which are defined in test_injector.js

compileComponents() : Promise<any>

Compile components with a templateUrl for the test's NgModule. It is necessary to call this function as fetching urls is asynchronous.

overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>) : typeof TestBed
overrideComponent(component: Type<any>, override: MetadataOverride<Component>) : typeof TestBed
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>) : typeof TestBed
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>) : typeof TestBed
get(token: any, notFoundValue?: any)
createComponent(component: Type<T>) : ComponentFixture<T>

Class Details

initTestEnvironment(ngModule: Type<any>, platform: PlatformRef)

Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite.

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

resetTestEnvironment()

Reset the providers for the test injector.

resetTestingModule()
platform : PlatformRef
ngModule : Type<any>
configureCompiler(config: {providers?: any[], useJit?: boolean})
configureTestingModule(moduleDef: TestModuleMetadata)
compileComponents() : Promise<any>
get(token: any, notFoundValue?: 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
createComponent(component: Type<T>) : ComponentFixture<T>

exported from @angular/core/testing/index, defined in @angular/core/testing/test_bed.ts

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v2.angular.io/docs/ts/latest/api/core/testing/index/TestBed-class.html