Moxios - TypeError:无法读取未定义的属性“适配器”

Leo*_*ban 3 testing axios moxios

尝试测试axios调用并尝试moxios包。

"axios": "^0.16.2", "moxios": "^0.4.0",

在这里找到:https : //github.com/axios/moxios

按照那里的例子,但我的测试错误就moxios.install()行了:

import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import { equal } from 'assert'

describe('mocking axios requests', function () {

  describe('across entire suite', function () {

    beforeEach(function () {
      // import and pass your custom axios instance to this method
      moxios.install()
    })
Run Code Online (Sandbox Code Playgroud)

我的实际测试

import axios from 'axios';
import moxios from 'moxios';
import sinon from 'sinon';
import { equal } from 'assert';

const akamaiData = {
  name: 'akamai'
};

describe('mocking axios requests', () => {
  describe('across entire suite', () => {
    beforeEach(() => {
      // import and pass your custom axios instance to this method
      moxios.install();
    });

    afterEach(() => {
      // import and pass your custom axios instance to this method
      moxios.uninstall();
    });

    it('should stub requests', (done) => {
      moxios.stubRequest('/akamai', {
        status: 200,
        response: {
          name: 'akamai'
        }
      });

      // const onFulfilled = sinon.spy();
      // axios.get('/akamai').then(onFulfilled);
      //
      // moxios.wait(() => {
      //   equal(onFulfilled.getCall(0).args[0], akamaiData);
      //   done();
      // });
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我确实在这里找到了这个关闭的问题,但是修复“传递axiosmoxios.install(axios)函数中不起作用”

https://github.com/axios/moxios/issues/15

小智 8

我遇到了同样的问题。原来我的axios.js文件__mocks__夹中有一个文件(从不同的模拟 axios 尝试中遗留下来的)。该模拟文件接管了实际的 axios 代码——但 moxios 需要真正的axios 代码才能正常运行。当我axios.js__mocks__文件夹中删除文件时,moxios 像宣传的那样工作。