0

我正在尝试模拟并尝试满足以下方法的其他条件,但我收到错误,因为预期的间谍 modalService.open 没有被调用

这是组件代码

更新以下行后 component.isError = true;

突出显示 If 块没有出现,但错误仍然可用

public importDeals(upload, list) {
  this.fileName = '';
   let ngbModalOptions: NgbModalOptions = {
    backdrop : 'static',
    keyboard : false,
    windowClass: 'custom-class'
};   
if (!this.isError) {
  this.uploadModalRef = this.modalService.open(upload, ngbModalOptions);
}
this.tempContingency = list;
}

下面是当前的单元测试用例(Jasmine)

it('should import deals', () => {
  // component.importDeals;
  // expect(component.importDeals('upload','list')).toBeUndefined();
  component.importDeals;
  component.uploadModalRef = jasmine.createSpyObj('uploadModalRef', ['close']);
  let mockOptions: NgbModalOptions = {
    backdrop : 'static',
    keyboard : false,
    windowClass: 'custom-class'
  };
  const mockConfirm = 'confirm-template';
  component.importDeals(mockConfirm,'');
  expect(modalService.open).toHaveBeenCalledWith(mockConfirm, mockOptions);
});
it('should not import deals', () => { 
  component.importDeals; 
  component.modalService = jasmine.createSpyObj('modalService',['open'])
  const mockConfirm = 'confirm-template'; 

  component.importDeals(mockConfirm,''); 
  expect(modalService.open).not.toHaveBeenCalled(); 
  });

请让我知道我在这里做错了什么

4

1 回答 1

1

将 的值设置isError为 true ,否则 if 块将始终执行。

it('should not import deals', () => { 
  component.importDeals; 
  component.modalService = jasmine.createSpyObj('modalService',['open']);
  component.isError = true; // <- this line
  const mockConfirm = 'confirm-template'; 

  component.importDeals(mockConfirm,''); 
  expect(modalService.open).not.toHaveBeenCalled(); 
});
于 2018-09-02T09:03:22.680 回答