尝试测试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();
// });
});
});
});

我在这里确实发现了这个封闭的问题,但是“将axios传递到moxios.install(axios)函数中”的修复程序没有工作。
发布于 2018-04-05 19:01:42
原来我不需要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');
});
});发布于 2018-04-02 18:45:28
我也有同样的问题。结果,我在我的axios.js文件夹中有一个__mocks__文件(来自不同的模拟axios的尝试)。这个模拟文件接管了实际的axios代码--但是艾莫斯需要真正的axios代码才能正常工作。当我从axios.js文件夹中删除__mocks__文件时,艾灸就像广告中所说的那样工作。
https://stackoverflow.com/questions/49414373
复制相似问题