我正在尝试为以下方法编写茉莉花测试用例

时间:2018-10-21 18:56:43

标签: angular karma-jasmine

onChange(event: any) {

if (event.type === 'drop') {
  this.file = event.dataTransfer.files;
} else {
  this.file = event.target.files;
}

for (let i = 0; i < this.file.length; i++) {
  if (this.formatsAllowed.includes('.' + this.file[i].name.split('.')[1])) {
    this.selectedFiles.push(this.file[i]);
  }
}
event.target.value = null;
}

规格:

it('should handle change event for includes property', () => {

  component.formatsAllowed = 'pdf';
  component.file;

  spyOn(component,'onChange').and.callThrough();
  component.onChange({
    type: 'default',
    target: {
      files : [],
      value: 'someVal'
    }
  });
  component.file.name='some.val';
  const x = formatsAllowed.includes('pdf');
  expect(x).toBeTruthy();
  expect(component.selectedFiles).toBeDefined();
});

我可以测试所有情况,除非条件为for循环。

我得到一个错误:

  

TypeError:无法设置未定义的属性“名称”

如果我删除并通过''价值测试用例已通过但未在Istanbul覆盖率报告中涵盖。

有人能弄清楚如何在这种情况下测试条件语句吗?

0 个答案:

没有答案
相关问题