【问题标题】:How to access the @ContentChildren in the spec file?如何访问规范文件中的@ContentChildren?
【发布时间】:2019-06-06 12:14:22
【问题描述】:

我正在测试一个具有@ContentChildren 查询列表的组件。在我的组件中,这些可以在“AfterContentInit”生命周期挂钩中访问。但是,当我打算在我的规范文件中访问相同的内容时,我的查询列表会返回一个空数组。

我正在尝试测试组件而不创建任何模拟组件,就像在 link 中所做的那样。我已经尝试了下面链接中提供的解决方案,但这对我不起作用。

Angular 2 unit testing - @ViewChild is undefined

在我的组件中:

    @ContentChildren(GetTemplateDirective) public templates: QueryList<GetTemplateDirective<AccordionTemplate>>;

在组件的规范文件中:

    it('should show the custom icon when the external template is passed', async(function () {
       accordionFixture.detectChanges();
       accordionComponent.ngAfterContentInit();
       console.log(accordionComponent.templates);
       expect(component.templates.toArray().length).toBeGreaterThan(0);
    }));

【问题讨论】:

    标签: angular jasmine karma-runner


    【解决方案1】:

    您可以使用调试元素进行查询:

     const debugEl = fixture.debugElement;
     debugEl.query(By.directive(AccordionTemplate));
    

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 2020-10-08
      • 2020-06-08
      • 2013-08-09
      • 1970-01-01
      • 2017-08-09
      相关资源
      最近更新 更多