mar*_*o07 8 unit-testing angular
我正在编写一个 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。”
有人知道如何解决这个问题吗?
Saa*_*aad 10
我将响应更改为 blob 类型:
let response = new Blob();
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力
| 归档时间: |
|
| 查看次数: |
4575 次 |
| 最近记录: |