Angular 8 单元测试错误 : 'Automatic conversion to Blob is not supported for response type.'

标签 angular unit-testing

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

      public getImage(link: string): Observable<any> {
    
        return this.http.get<any>(this.createUrl('storage/' + link ), { headers: this.createHeaders(), 
        responseType: 'blob' as 'json' });
      }
这是我的单元测试:
  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);
     
      });     
  });
当我像上面那样编写测试时,我收到错误:“预期没有打开的请求,发现 1:”
如果我把这个代码:
const req = httpMock.expectOne( https://api/storage/testImage);
期望(req.request.method).toBe('GET');
req.flush(响应);
在订阅之外,我收到错误消息:“响应类型不支持自动转换为 Blob。”
有人知道如何解决这个问题吗?

最佳答案

我将响应更改为 blob 类型:

 let response = new Blob();
它就像一个魅力😋

关于Angular 8 单元测试错误 : 'Automatic conversion to Blob is not supported for response type.' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63615321/

相关文章:

angular - angular2中动态加载外部模块

html - Angular - 选择占位符不显示

node.js - sinon stub 在 Node.js 测试中不起作用

python - Unittest 采用 1 个位置参数,但给出了 2 个

java - JUnit 断言失败

javascript - Angular:将动态元素滚动到只知道其 ID 的 View 中

javascript - Angular 2 获取 header 响应

javascript - 无法在 Visual Studio Code 中使用 TypeScript 设置 Angular 2

eclipse - 从SpringSource Tool Suite(STS)运行集中的Groovy单元测试

javascript - 在 NodeJs 中使用 Jest 模拟 Http Post 请求