首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Moxios - TypeError:无法读取未定义的属性“适配器”

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

Stack Overflow用户
提问于 2018-03-21 18:47:32
回答 2查看 4.6K关注 0票数 3

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

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

在此发现:https://github.com/axios/moxios

下面是示例,但是我的测试错误出现在moxios.install()行上:

代码语言:javascript
复制
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()
    })

我的实际测试

代码语言:javascript
复制
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();
      // });
    });
  });
});

我在这里确实发现了这个封闭的问题,但是“将axios传递到moxios.install(axios)函数中”的修复程序没有工作。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-05 19:01:42

原来我不需要moxios,在我的测试中我不想做一个实际的API调用.只是需要确保函数被调用了。用测试函数修正了它。

代码语言:javascript
复制
import { makeRequest } from 'utils/services';
import { getImages } from './akamai';

global.console = { error: jest.fn() };

jest.mock('utils/services', () => ({
  makeRequest: jest.fn(() => Promise.resolve({ data: { foo: 'bar' } }))
}));

describe('Akamai getImages', () => {
  it('should make a request when we get images', () => {
    getImages();
    expect(makeRequest).toHaveBeenCalledWith('/akamai', 'GET');
  });
});
票数 -3
EN

Stack Overflow用户

发布于 2018-04-02 18:45:28

我也有同样的问题。结果,我在我的axios.js文件夹中有一个__mocks__文件(来自不同的模拟axios的尝试)。这个模拟文件接管了实际的axios代码--但是艾莫斯需要真正的axios代码才能正常工作。当我从axios.js文件夹中删除__mocks__文件时,艾灸就像广告中所说的那样工作。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49414373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档