如何编写单元测试,以测试从API下载文件的服务

时间:2019-04-11 06:53:02

标签: unit-testing angular7

我正在编写单元测试以测试从API下载文件的服务

以下我用来下载文件的代码。工作正常。我不确定如何为此编写单元测试用例。

估算卡组件

public getEstimateFile(estimateFileName: string): void {
  this.estimateFileName = estimateFileName;
  this.estimateService.getEstimateFileDetails(this.workOrderNum, 
  this.estimateFileName)
    .subscribe((result: Blob) =>
      this.downloadFile(result)
    );
  }


  private downloadFile(data: Blob): void {
    const blob = new Blob([data], { type: 'application/pdf;charset=utf-8;' });
    const link = document.createElement('a');
    link.style.display = 'none';
    document.body.appendChild(link);
    if (link.download) {
      link.setAttribute('href', URL.createObjectURL(blob));
      link.setAttribute('download', this.estimateFileName);
      link.click();
    }
    document.body.removeChild(link);
  }

估算服务

 public getEstimateFileDetails(workorderNum: string, estimateFileName: string): Observable<Blob> {
    const url = `${this.baseUrl}/${ResourceUrl.workOrderDetail}/` + workorderNum + '/Estimate/' + estimateFileName;
    const headers = new HttpHeaders().set('Content-Type', 'application/pdf');
    return this.httpClient.get(url, { headers: headers, responseType: 'blob' as 'json' }).pipe(
        map((estimateFile: Blob) => {
            return estimateFile;
        }));
  }

我要为上述代码编写单元测试用例以下载文件。

0 个答案:

没有答案