[英]Angular 2 TestModuleMetadata does not have an EntryComponents property
我正在使用Angular CLI 1.0.0-beta.32.2並正在為Angular 2服務編寫單元測試,該服務動態創建一個Component並將其插入另一個Component。
在我的單元測試中,我嘗試創建一個模擬組件來測試我的服務,但ng test
的輸出拋出一個錯誤,說明我的模擬組件在entryComponents
屬性中指定。 當我嘗試將Component添加到TestModuleMetadata對象的entryComponents
屬性中時,如下所示: TestBed.createTestingModule({...entryComponents: [ TestDialogComponent ]...})
我看到以下錯誤說明entryComponents
屬性不存在。
Chrome 56.0.2924 (Windows 10 0.0.0) DialogService should create a child component when opening FAILED Error: No component factory found for TestDialogComponent. Did you add it to @NgModule.entryComponents?
查看TestModuleMetadata定義顯示entryComponents屬性不存在。 那么如何在Angular 2和Jasmine的單元測試中動態創建一個Component呢?
據我所知,它還沒有得到支持。 作為解決方法,您可以使用entryComponent
創建假模塊並將其導入測試模塊
@NgModule({
imports: [CommonModule],
declarations: [TestDialogComponent],
entryComponents: [TestDialogComponent]
})
export class FakeTestDialogModule {}
然后
TestBed.configureTestingModule({
imports: [FakeTestDialogModule]
檢查TestBed.overrideModule
。 請參閱此討論https://github.com/angular/angular/issues/12079
我收到了DialogBoxComponent
的錯誤。 我解決了如下問題
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ NewPracticeQuestionComponent,
DialogBoxComponent,
ShowErrorsComponent],
imports:[ReactiveFormsModule,HttpClientTestingModule],
providers:[WebToBackendInterfaceService,HelperService,AuthService]
})
TestBed.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [DialogBoxComponent]
}
})
.compileComponents();
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.