小编mar*_*o07的帖子

Angular8 Unitest 错误:“响应类型不支持自动转换为 Blob。”

我正在编写一个 httpGet 调用来获取 blob 图像,这里的调用是:

      public getImage(link: string): Observable<any> {
    
        return this.http.get<any>(this.createUrl('storage/' + link ), { headers: this.createHeaders(), 
        responseType: 'blob' as 'json' });
      }
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试:

  it('should get the images', () => {
    let response = {};
    service.getImage('testImage').subscribe(
      data => {
        expect(data).toEqual(response, 'should return expected data');

        const req = httpMock.expectOne(`https://api/storage/testImage`);
        expect(req.request.method).toBe('GET');
        req.flush(response);
     
      });     
  });
Run Code Online (Sandbox Code Playgroud)

当我像上面那样编写测试时,我收到错误:“预计没有打开的请求,发现 1:”

如果我输入这段代码:

const req = httpMock.expectOne(https://api/storage/testImage ); 期望(req.request.method).toBe('GET'); 请求刷新(响应);

在订阅之外,我收到错误:“响应类型不支持自动转换为 Blob。”

有人知道如何解决这个问题吗?

unit-testing angular

8
推荐指数
1
解决办法
4575
查看次数

标签 统计

angular ×1

unit-testing ×1