Moxios - TypeError: Cannot read property 'adapter' of undefined
Moxios - TypeError: Cannot read property 'adapter' of undefined
正在尝试测试 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()
})
我的实测
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();
// });
});
});
});
我确实在这里找到了这个已解决的问题,但是修复 "passing axios
into the moxios.install(axios)
function did not work"
我遇到了同样的问题。原来我的 __mocks__
文件夹中有一个 axios.js
文件(来自另一次模拟 axios 的尝试的遗留物)。该模拟文件接管了实际的 axios 代码——但 moxios 需要 real axios 代码才能正常运行。当我从 __mocks__
文件夹中删除 axios.js
文件时,moxios 像宣传的那样工作。
事实证明我不需要 moxios
,在我的测试中我不想进行实际的 API 调用...只是需要确保调用该函数。用测试功能修复它。
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');
});
});
正在尝试测试 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()
})
我的实测
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();
// });
});
});
});
我确实在这里找到了这个已解决的问题,但是修复 "passing axios
into the moxios.install(axios)
function did not work"
我遇到了同样的问题。原来我的 __mocks__
文件夹中有一个 axios.js
文件(来自另一次模拟 axios 的尝试的遗留物)。该模拟文件接管了实际的 axios 代码——但 moxios 需要 real axios 代码才能正常运行。当我从 __mocks__
文件夹中删除 axios.js
文件时,moxios 像宣传的那样工作。
事实证明我不需要 moxios
,在我的测试中我不想进行实际的 API 调用...只是需要确保调用该函数。用测试功能修复它。
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');
});
});