繁体   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