我正在编写一个 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/