Angular 如何对作为viewchild的函数进行单元测试
我有一个场景,需要测试一个函数,该函数作为Angular 如何对作为viewchild的函数进行单元测试,angular,typescript,unit-testing,jasmine,karma-jasmine,Angular,Typescript,Unit Testing,Jasmine,Karma Jasmine,我有一个场景,需要测试一个函数,该函数作为@Viewchild引用 My component.ts @ViewChild('accTitle', { read: ViewContainerRef, static: false }) title: ViewContainerRef; @ViewChild('accText', { read: ViewContainerRef, static: false }) body: ViewContainerRef; constru
@Viewchild
引用
My component.ts
@ViewChild('accTitle', { read: ViewContainerRef, static: false }) title: ViewContainerRef;
@ViewChild('accText', { read: ViewContainerRef, static: false }) body: ViewContainerRef;
constructor(
private renderEngineService: RenderEngineService,
@Inject(CONTENT_MAPPINGS) private contentMappings: any,
private changeDetectorRef: ChangeDetectorRef
) {}
ngAfterViewInit() {
this.renderEntityRanges(this.item);
}
renderEntityRanges(item: AreasData) {
this.item.sections.forEach(section => {
if (section.type === 'title') {
this.renderEngineService.setRootViewContainerRef(this.title);
} else if (section.type === 'text') {
this.renderEngineService.setRootViewContainerRef(this.body);
}
section.rjf.forEach(rjf => {
const type = this.contentMappings[rjf.type];
this.renderEngineService.createComponent(rjf, type);
this.changeDetectorRef.detectChanges();
});
});
}
所以在我的规范文件中,我做了以下工作
it('should invoke renderEntityRanges', () => {
const renderEntityRanges = spyOn(accordionItemComponent, 'renderEntityRanges');
accordionItemComponent.ngAfterViewInit();
expect(renderEntityRanges).toHaveBeenCalledWith(ACCORDION_ITEM);
});
上面的测试用例运行良好
但是我现在确定了如何测试这个函数renderEntityRanges
它的作用是renderEntityRanges
,它将检查ACCORDION\u项中的类型
,如果类型
是标题
,则将对this.renderEngineService.setRootViewContainerRef进行服务调用,输入为this.title
因此,请帮助我们如何对这个函数进行单元测试 为了更正renderEntityRanges
,我仅将此.item
删除为item
,因为它是作为参数传递的
renderEntityRanges(item: AreasData) {
item.sections.forEach(section => {
if (section.type === 'title') {
this.renderEngineService.setRootViewContainerRef(this.title);
} else if (section.type === 'text') {
this.renderEngineService.setRootViewContainerRef(this.body);
}
section.rjf.forEach(rjf => {
const type = this.contentMappings[rjf.type];
this.renderEngineService.createComponent(rjf, type);
this.changeDetectorRef.detectChanges();
});
});
}
并将renderEngineService
和changeDetectorRef
作为公共
在构造函数中进行间谍活动:
public renderEngineService: RenderEngineService,
public changeDetectorRef: ChangeDetectorRef
您可以尝试按以下方式进行测试:
it('should set Container Ref and create component',()=>{
spyOn(accordionItemComponent.renderEngineService,'setRootViewContainerRef').and.callThrough();
spyOn(accordionItemComponent.renderEngineService,'createComponent').and.callThrough();
spyOn(accordionItemComponent.changeDetectorRef,'detectChanges').and.callThrough();
accordionItemComponent.renderEntityRanges(ACCORDION_ITEM);
expect(accordionItemComponent.renderEngineService.setRootViewContainerRef).toHaveBeenCalled();
expect(accordionItemComponent.renderEngineService.createComponent).toHaveBeenCalled();
expect(accordionItemComponent.changeDetectorRef.detectChanges).toHaveBeenCalled();
})
并确保ACCORDION\u项
具有同时满足类型
和section.rjf=[要迭代的数组]