我有一个组件,它接收component的component类,以动态创建为一个子组件。
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentToCreate);
this.componentReference = this.target.createComponent(componentFactory);我正在尝试编写一个单元测试,并为它传递一些TestComponent来创建和呈现。
TestBed
.configureTestingModule(<any>{
declarations: [MyAwesomeDynamicComponentRenderer, TestHostComponent],
entryComponents: [TestComponent],
});转换为"any“是因为configureTestingModule需要没有entryComponents的TestModuleMetadata,但我得到了错误:”没有为TestComponent找到组件工厂“。
如何向TestBed提供entryComponents
发布于 2017-08-07 23:34:27
如果需要,您也可以直接在测试文件中执行此操作:
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing'; // DO not forget to Import
TestBed.configureTestingModule({
declarations: [ MyDynamicComponent ],
}).overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [ MyDynamicComponent ],
}
});发布于 2017-01-05 20:01:05
好吧,我想通了。在测试中,您应该定义新模块,在其中声明您的模拟组件,并将其指定为entryComponent。
@NgModule({
declarations: [TestComponent],
entryComponents: [
TestComponent,
]
})
class TestModule {}并将其导入到TestBed
TestBed
.configureTestingModule({
declarations: [ValueComponent, TestHostComponent],
imports: [TestModule],
});我希望它能帮助某些人:]
https://stackoverflow.com/questions/41483841
复制相似问题