我有一个重写的 Axios 函数,它可以帮助我发出 API 请求。该函数的实现是当 API 调用失败时,它会在error.customAttributes
.
try {
const data = await getMeDataPlease(param);
} catch (error) {
if (error.customAttributes.httpErrorStatus === 404) {
error.userError = {} // anything
throw error;
}
}
我想测试这个函数catch
代码,它正确地添加/构建error.userError
对象。为此,我嘲笑 Axios 调用如下。
describe('External Apis', () => {
it('Get me data fails', async () => {
const axiosMock = new MockAdapter(myCustomHttpClient());
const url = 'my-data-url-goes-here';
axiosMock.onGet(url).reply(404, {customAttributes: {httpErrorStatus:404});
const data = await getEnrollments('username'); // in actual method the method fails on `if (error.customAttributes.httpErrorStatus === 404)` with undefined does not have `httpErrorStatus`
console.log('data', data);
});
但我无法像那样模拟 Axios 请求,错误对象不包含我期望customAttributes
的 . 好心提醒。