NgTestBed<T extends Object> constructor

NgTestBed<T extends Object>(
  1. ComponentFactory<T> component, {
  2. Element? host,
  3. InjectorFactory rootInjector = _defaultRootInjector,
  4. bool watchAngularLifecycle = true,
})

Create a new NgTestBed that uses the provided component factory.

Some APIs are not supported outside of NgTestBed.useInitReflector:

  • addProviders will throw UnsupportedError; instead, the addInjector API allows you to wrap the previous Injector, if any, to provide additional services. In most cases just rootInjector is enough, and you could re-use providers via GenerateInjector.
void main() {
  final ngTestBed = NgTestBed(
    SomeComponentNgFactory,
    rootInjector: ([parent]) => new Injector.map({
      Service: new Service(),
    }, parent),
  );
}

Implementation

factory NgTestBed(
  ComponentFactory<T> component, {
  Element? host,
  InjectorFactory rootInjector = _defaultRootInjector,
  bool watchAngularLifecycle = true,
}) {
  if (T == dynamic) {
    throw GenericTypeMissingError();
  }
  return NgTestBed<T>._useComponentFactory(
    component: component,
    rootInjector: rootInjector,
    host: host,
    watchAngularLifecycle: watchAngularLifecycle,
  );
}