【发布时间】:2019-03-07 04:39:27
【问题描述】:
我发出了一个失败的 axios 请求,并在 catch 块中抛出错误。 在 catch 块中,我正在向其他端点发出请求以获取结果
router.get('/info', (req, res) => {
axios('www.getInformationApi.com')
.then(results => {
return res.status(200).json(results.data);
})
.catch(async(err) => {
if (err === 'server_down') {
try {
// need to improve test coverage for this lines
const secondsResults = await axios('www.getInformationApi2.com');
return res.status(200).json(secondsResults)
} catch (error) {
throw Error(error)
}
} else {
const error = errors.createEntError(err.message, errorList);
return res.status(constants.HTTP_SERVER_ERROR).json(error);
}
})
});
我喜欢使用“nock”或“http-mocks”在 catch 块中编写涵盖“Axios”请求的单元测试
我能否使用单元测试进行测试,或者我需要在这种情况下运行集成测试
【问题讨论】:
-
第一个 axios 请求的示例测试用例 codepen.io/punith77/pen/bmVBWB?editors=0010
标签: javascript node.js chai nock http-mock