node.js - 如何在 sinon 中测试 axios 的调用获取?
问题描述
我想知道如何用 sinon 测试 axios 库。如果有人可以帮助我,我找不到解决方案。
太感谢了!
这是文件代码:
const {get} = require('axios');
const getToken = async (_req, res) => {
const functionURL = 'test1';
const tokenUrl = 'urlTest';
const tokenResponse = await get(tokenUrl, {
headers: {
'Metadata-Flavor': 'Google',
},
});
res.status(200).send(tokenResponse);
}
module.exports = {
getToken
};
这是文件测试:
let axios = require('axios');
describe('GetToken unit tests', async () => {
it('should get a valid token for google', async () => {
axios = sinon.stub(axios, "get").resolves('ey..');
const response = await getToken();
sinon.assert.match(response,'ey..');
});
});
解决方案
推荐阅读
- reactjs - 调整 MUI 中自动完成下拉菜单的字体大小
- javascript - React JS 上下文,TypeError:无法读取未定义的属性
- python - Python 无法下载带有无扩展名的动态 URL 的 excel 文件
- android - 如何使用 Jetpack Compose 使 LinearProgressIndicator 宽度动态化
- javascript - React.js 中的 Axios 网络错误 createError.js:16
- javascript - 如何在不和谐中找到消息的作者,以及如何使用它来确定它是否具有命令的正确权限
- javascript - VictoryChart 不更改日期数据
- ios - 修复 iOS 15 UINavigationBar
- android - Kotlin - 事件前的 WorkManager 通知
- html - TailwindCSS - 将所有网格项目设置为相同的高度