【发布时间】: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