如何访问规范文件中的@ContentChildren?

时间:2019-01-11 14:47:22

标签: angular jasmine karma-runner

我正在测试查询列表为@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);
    }));

1 个答案:

答案 0 :(得分:0)

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

 const debugEl = fixture.debugElement;
 debugEl.query(By.directive(AccordionTemplate));
相关问题