Angular 带有(事件:HttpEvent<;any>;)参数的角度文件上载单元测试无法';不包括完整代码

Angular 带有(事件:HttpEvent<;any>;)参数的角度文件上载单元测试无法';不包括完整代码,angular,jasmine,karma-jasmine,Angular,Jasmine,Karma Jasmine,我用的是第九版。在我的应用程序中,我有一个文件上传方法,该方法具有显示文件上传进度的功能 代码已覆盖到第一个交换机案例(案例HttpEventType.Sent),无法覆盖剩余代码 ts: 您的订单似乎不正确,在调用component.uploadFile之前,您必须spyOn服务uploadFileModel it('should create upload template', () => { //component.selectedModel = { id: 2, name:

我用的是第九版。在我的应用程序中,我有一个文件上传方法,该方法具有显示文件上传进度的功能

代码已覆盖到第一个交换机案例(案例HttpEventType.Sent),无法覆盖剩余代码

ts:


您的订单似乎不正确,在调用
component.uploadFile
之前,您必须
spyOn
服务
uploadFileModel

it('should create upload template', () => {
    //component.selectedModel = { id: 2, name: 'NER', code: 'NER' };
    const mockFile = [ { 0: { name: 'foo', size: 500001 }, 1: { name: 'foo', size: 500001 } }];

    // spyOn(component, 'uploadFile').and.callThrough(); // get rid of this line
    spyOn(service, 'uploadFileModel').and.returnValue(of( // move this here
      {
        // mock the return here
        type: HttpEventType.Sent, // This time Sent will be traversed.
        body: {}
        status: 200,
        loaded: 0,
      }
    ));
    component.uploadFile();
    fixture.detectChanges();
    // expect(component.uploadFile).toHaveBeenCalled(); // get rid of this line, we explicitly called it so obviously it was called.
    expect(service.uploadFileModel).toBeTruthy();
  });

要遍历其他情况,请编写另一个单元测试(it块)并更改模拟返回的类型。

如何遍历开关情况以覆盖100%代码,如何检查http事件类型!
it('should create upload template', () => {
    //component.selectedModel = { id: 2, name: 'NER', code: 'NER' };
    const mockFile = [ { 0: { name: 'foo', size: 500001 }, 1: { name: 'foo', size: 500001 } }];

    spyOn(component, 'uploadFile').and.callThrough();
    
    component.uploadFile();
    fixture.detectChanges();
    spyOn(service, 'uploadFileModel').and.returnValue(of(
      {
        result : {
           httpStatus : 200
        }
      }
    ));
    fixture.detectChanges();
    expect(component.uploadFile).toHaveBeenCalled();
    expect(service.uploadFileModel).toBeTruthy();
  });
it('should create upload template', () => {
    //component.selectedModel = { id: 2, name: 'NER', code: 'NER' };
    const mockFile = [ { 0: { name: 'foo', size: 500001 }, 1: { name: 'foo', size: 500001 } }];

    // spyOn(component, 'uploadFile').and.callThrough(); // get rid of this line
    spyOn(service, 'uploadFileModel').and.returnValue(of( // move this here
      {
        // mock the return here
        type: HttpEventType.Sent, // This time Sent will be traversed.
        body: {}
        status: 200,
        loaded: 0,
      }
    ));
    component.uploadFile();
    fixture.detectChanges();
    // expect(component.uploadFile).toHaveBeenCalled(); // get rid of this line, we explicitly called it so obviously it was called.
    expect(service.uploadFileModel).toBeTruthy();
  });