首页 > 解决方案 > 如何在 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..');
      });
    });

标签: node.jsaxiossinon

解决方案


推荐阅读