带有嘲笑的轴突和kitsu

问题描述 投票:0回答:1

[我正在创建一些测试,并且使用Kitsu(https://github.com/wopian/kitsu/tree/master/packages/kitsu)作为使用API​​的客户端。

我需要模拟API请求,但不能。 Kitsu在内部使用axios,但我不知道如何模拟结构:

Kitsu {
  camel: [Function: index],
  resCase: [Function: index$1],
  plural: [Function: pluralize] {
    plural: [Function],
    isPlural: [Function],
    singular: [Function],
    isSingular: [Function],
    addPluralRule: [Function],
    addSingularRule: [Function],
    addUncountableRule: [Function],
    addIrregularRule: [Function]
  },
  headers: {
    Accept: 'application/vnd.api+json',
    'Content-Type': 'application/vnd.api+json'
  },
  axios: [Function: wrap] {
    request: [Function: wrap],
    getUri: [Function: wrap],
    delete: [Function: wrap],
    get: [Function: wrap],

我需要在axios属性中模拟get方法。我如何使用笑话来做到这一点?谢谢

javascript jest
1个回答
0
投票

您可以使用axios-mock-adapterhttps://www.npmjs.com/package/axios-mock-adapter

例如

const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const mock = new MockAdapter(axios);

mock.onGet('http://somehost/path').reply(200, 'response goes here');
© www.soinside.com 2019 - 2024. All rights reserved.