簡體   English   中英

Angular 2 TestModuleMetadata沒有EntryComponents屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM