我有一个从服务器接收文件的 fetch,我正在尝试使用fetch-mock
.
使用此代码,我可以模拟端点并将 blob 放在正文中:
const blob = new Blob(['a', 'b', 'c', 'd']);
fetchMock.once('*', {body: blob}, {sendAsJson: false});
正在测试的代码是:
fetch(url).then( ( response ) => {
console.log(response);
return response.blob();
} ).then( ( blob ) => {
console.log(blob);
} )
我可以看到 Blob 在请求的正文中
Body {
url: '/mock/url',
status: 200,
statusText: 'OK',
headers: Headers { _headers: {} },
ok: true,
body: Blob {},
bodyUsed: false,
size: 0,
timeout: 0,
_raw: [],
_abort: false }
但是运行测试会引发错误:
TypeError: response.blob is not a function
使用服务器运行代码会将有效的 Blob 返回到最终的.then
.