为单元测试茉莉写If else



我刚开始学习茉莉花。我很困惑,如果我需要测试函数,值也是函数,就像这样关于组件.ts

validationLoadListDownload() {
if (this.getGroupId === 0) {
this.loadListDownloadRequest();
} else {
this.loadListDownloadRequestGroup();
}
}

如果是这样的话,我们如何在component.spec.ts上编写它?我很困惑,谢谢

可以这样做;

it('should download the validation list when getGroupId is zero', () => {
const loadListDownloadRequestSpy = spyOn(component, 'loadListDownloadRequest');
component.getGroupId = 0;
component.validationLoadListDownload();
expect(loadListDownloadRequestSpy).toHaveBeenCalled();
});

it('should download the validation list when getGroupId is not zero', () => {
const loadListDownloadRequestGroupSpy = spyOn(component, 'loadListDownloadRequestGroup');
component.getGroupId = 5;
component.validationLoadListDownload();
expect(loadListDownloadRequestGroupSpy ).toHaveBeenCalled();
});

最新更新