【问题标题】:Angular 2 TestModuleMetadata does not have an EntryComponents propertyAngular 2 TestModuleMetadata 没有 EntryComponents 属性
【发布时间】:2017-07-12 21:21:49
【问题描述】:

我正在使用 Angular CLI 1.0.0-beta.32.2 并正在为 Angular 2 服务编写单元测试,该服务动态创建一个组件并将其插入另一个组件。

在我的单元测试中,我尝试创建一个模拟组件来测试我的服务,但是ng test 的输出抛出一个错误,说明我的模拟组件是在entryComponents 属性中指定的。当我尝试将组件添加到 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 的单元测试中动态创建组件呢?

【问题讨论】:

    标签: unit-testing angular typescript angular-cli


    【解决方案1】:

    据我所知,它还不支持。作为解决方法,您可以使用 entryComponent 创建假模块并将其导入您的测试模块

    @NgModule({
      imports: [CommonModule],
      declarations: [TestDialogComponent],
      entryComponents: [TestDialogComponent]
    })
    export class FakeTestDialogModule {}
    

    然后

    TestBed.configureTestingModule({
        imports: [FakeTestDialogModule]
    

    【讨论】:

    【解决方案2】:

    检查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();
      }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 2020-08-03
      • 2017-06-20
      • 2020-10-18
      • 2017-02-06
      • 2017-09-20
      • 2017-03-15
      相关资源
      最近更新 更多