本文介绍了提供“entryComponents”对于TestBed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件,它接收组件的组件类,以动态创建为子组件。

I have a component which receives a component class of component to dynamically create as a child.

let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentToCreate);
this.componentReference = this.target.createComponent(componentFactory);

我正在尝试编写单元测试并传递一些TestComponent来创建&渲染。

I'm trying to write a unit test and pass some TestComponent for it to create & render.

TestBed
  .configureTestingModule(<any>{
    declarations: [MyAwesomeDynamicComponentRenderer, TestHostComponent],
    entryComponents: [TestComponent],
  });

铸造为any因为 configureTestingModule 期望 TestModuleMetadata 哪个没有 entryComponents 但是我收到错误:找不到TestComponent的组件工厂。

There is casting to "any" because configureTestingModule expects TestModuleMetadata which doesn't have entryComponents but I get error: "No component factory found for TestComponent".

如何将 entryComponents 提供给 TestBed

推荐答案

如果需要,您也可以直接在测试文件中输入:

You can also do it into your test file directly if you want :

TestBed.configureTestingModule({
  declarations: [ MyDynamicComponent ],
}).overrideModule(BrowserDynamicTestingModule, {
  set: {
    entryComponents: [ MyDynamicComponent ],
  }
});

这篇关于提供“entryComponents”对于TestBed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:24