单元测试接收到带有$httpBackend的二进制响应

unit test receiving binary response with $httpBackend

本文关键字:httpBackend 二进制 响应 单元测试      更新时间:2023-09-26

我有一个服务,它处理来自web API调用的二进制响应:

$http.get('/endpoint', {
    params: urlparams,
    transformResponse: transform,
    responseType: 'arraybuffer'
});

我正在尝试使用$httpBackend:进行测试

$httpBackend.expectGET('/endpoint').respond(200, data);

但它不起作用,因为数据会自动转换为JSON,而且我找不到在expectGET调用中指定responseType的方法。

如何使expectGET$http.get调用提供二进制响应?我尝试过将data转换为ArrayBuffer,但没有成功。

我也遇到过类似的问题,并使用了:

$httpBackend.expectGET('/endpoint').respond(200, new Blob([data]));

然而,我不得不将PhantomJs从1.x更新为2.x才能工作。