简体   繁体   English

Angular 2 TestModuleMetadata没有EntryComponents属性

[英]Angular 2 TestModuleMetadata does not have an EntryComponents property

I'm using Angular CLI 1.0.0-beta.32.2 and am writing a unit test for an Angular 2 service, which dynamically creates a Component and inserts it into another Component. 我正在使用Angular CLI 1.0.0-beta.32.2并正在为Angular 2服务编写单元测试,该服务动态创建一个Component并将其插入另一个Component。

In my unit test I try to create a mock component to test my service, but the output of ng test throws an error, stating my mock Component is specified in the entryComponents property. 在我的单元测试中,我尝试创建一个模拟组件来测试我的服务,但ng test的输出抛出一个错误,说明我的模拟组件在entryComponents属性中指定。 When I try to add the Component into an entryComponents property of the TestModuleMetadata object, like this: TestBed.createTestingModule({...entryComponents: [ TestDialogComponent ]...}) I see the following error stating the entryComponents property does not exist. 当我尝试将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?

Looking at the TestModuleMetadata definition shows that the entryComponents property does not exist. 查看TestModuleMetadata定义显示entryComponents属性不存在。 So how do I go about dynamically creating a Component in my unit tests in Angular 2 and Jasmine? 那么如何在Angular 2和Jasmine的单元测试中动态创建一个Component呢?

As far as i know it has not supported yet. 据我所知,它还没有得到支持。 As workaround you can create fake module with entryComponent and import it to your testing module 作为解决方法,您可以使用entryComponent创建假模块并将其导入测试模块

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

and then 然后

TestBed.configureTestingModule({
    imports: [FakeTestDialogModule]

Check TestBed.overrideModule . 检查TestBed.overrideModule Please refer to this discussion https://github.com/angular/angular/issues/12079 请参阅此讨论https://github.com/angular/angular/issues/12079

I got the error for my DialogBoxComponent . 我收到了DialogBoxComponent的错误。 I resolved it as follows 我解决了如下问题

  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