axios-mock-adapter onGet 模拟数据无效
axios-mock-adapter onGet mock data not effective
我正在尝试使用 axios-mock-adapter 测试 axios 调用。我遇到了以下问题:
来自测试的 API 调用总是响应真实数据,而不是我用 mock.onGet 模拟的数据。
a.k。 receivedActions 总是来自真正的 API 调用,而不是用 mock.onGet 模拟的 expectedActions。
这是操作代码(searchAction.js):
import { SEARCH_PHOTOS, FEATURED_PHOTOS } from './types';
import axiosInstence from '../apis/axiosInstence';
export const searchPhotos = (term) => (dispatch) => {
dispatch({ type: 'SEACH_REQUEST' });
return axiosInstence.get('/search/photos', {
params: {
query: term,
page: 1
}
}).then(response => {
dispatch({
type: 'SEARCH_PHOTOS',
payload: response.data
});
}).catch(error => {
dispatch({ type: 'SEACH_FAILURE' });
});
}
我的测试是这样的 (searchAction.test.js):
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import { searchPhotos } from '../searchAction';
const mock = new MockAdapter(axios);
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const term = 'cars';
const store = mockStore({});
describe('actions', () => {
beforeEach(() => {
mock.reset();
store.clearActions();
});
it('Should create an action to signIn with a fake user', async () => {
const expectedActions = [{
type: 'SEACH_REQUEST'
}, {
type: 'SEARCH_PHOTOS',
payload: []
}];
mock.onGet('/search/photos', {
params: { term: 'cars' }
}).reply(200, expectedActions);
await store.dispatch(searchPhotos(term))
.then(data => {
const receivedActions = store.getActions();
expect(receivedActions).toEqual(expectedActions);
});
});
});
有谁遇到过类似的问题或者可以给我一些建议。
提前致谢。
您的代码中有几个问题:
First,在动作创建器中,您使用 axios 实例进行 ajax 调用,但在测试中,您没有将该实例提供给 axios-mock-adapter
。当你创建 MockAdapter
.
的实例时,你应该在你的测试中提供你的 axios 实例
其次,你在onGet
方法中提供给axios mock的params
属性不匹配参数在您的操作创建器中的 get
操作中发送。您应该将调用中的参数与其值相匹配。因此,您应该提供 query
和 page
参数。
Last,您正在 returning 模拟请求中的 expectedActions
,但这似乎不正确。查看您的代码,您似乎想要 return 一个空数组。
考虑到所有这些因素,您的代码将如下所示:
import MockAdapter from 'axios-mock-adapter';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import axiosInstence from '../../apis/axiosInstence';
import { searchPhotos } from '../searchAction';
const mock = new MockAdapter(axiosInstence);
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const term = 'cars';
const store = mockStore({});
describe('actions', () => {
beforeEach(() => {
mock.reset();
store.clearActions();
});
it('Should create an action to signIn with a fake user', async () => {
const expectedActions = [{
type: 'SEACH_REQUEST'
}, {
type: 'SEARCH_PHOTOS',
payload: []
}];
mock.onGet('/search/photos', {
params: {
query: 'cars',
page: 1
}
}).reply(200, []);
const data = await store.dispatch(searchPhotos(term));
const receivedActions = store.getActions();
expect(receivedActions).toEqual(expectedActions);
});
});
我正在尝试使用 axios-mock-adapter 测试 axios 调用。我遇到了以下问题: 来自测试的 API 调用总是响应真实数据,而不是我用 mock.onGet 模拟的数据。 a.k。 receivedActions 总是来自真正的 API 调用,而不是用 mock.onGet 模拟的 expectedActions。 这是操作代码(searchAction.js):
import { SEARCH_PHOTOS, FEATURED_PHOTOS } from './types';
import axiosInstence from '../apis/axiosInstence';
export const searchPhotos = (term) => (dispatch) => {
dispatch({ type: 'SEACH_REQUEST' });
return axiosInstence.get('/search/photos', {
params: {
query: term,
page: 1
}
}).then(response => {
dispatch({
type: 'SEARCH_PHOTOS',
payload: response.data
});
}).catch(error => {
dispatch({ type: 'SEACH_FAILURE' });
});
}
我的测试是这样的 (searchAction.test.js):
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import { searchPhotos } from '../searchAction';
const mock = new MockAdapter(axios);
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const term = 'cars';
const store = mockStore({});
describe('actions', () => {
beforeEach(() => {
mock.reset();
store.clearActions();
});
it('Should create an action to signIn with a fake user', async () => {
const expectedActions = [{
type: 'SEACH_REQUEST'
}, {
type: 'SEARCH_PHOTOS',
payload: []
}];
mock.onGet('/search/photos', {
params: { term: 'cars' }
}).reply(200, expectedActions);
await store.dispatch(searchPhotos(term))
.then(data => {
const receivedActions = store.getActions();
expect(receivedActions).toEqual(expectedActions);
});
});
});
有谁遇到过类似的问题或者可以给我一些建议。 提前致谢。
您的代码中有几个问题:
First,在动作创建器中,您使用 axios 实例进行 ajax 调用,但在测试中,您没有将该实例提供给 axios-mock-adapter
。当你创建 MockAdapter
.
其次,你在onGet
方法中提供给axios mock的params
属性不匹配参数在您的操作创建器中的 get
操作中发送。您应该将调用中的参数与其值相匹配。因此,您应该提供 query
和 page
参数。
Last,您正在 returning 模拟请求中的 expectedActions
,但这似乎不正确。查看您的代码,您似乎想要 return 一个空数组。
考虑到所有这些因素,您的代码将如下所示:
import MockAdapter from 'axios-mock-adapter';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import axiosInstence from '../../apis/axiosInstence';
import { searchPhotos } from '../searchAction';
const mock = new MockAdapter(axiosInstence);
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const term = 'cars';
const store = mockStore({});
describe('actions', () => {
beforeEach(() => {
mock.reset();
store.clearActions();
});
it('Should create an action to signIn with a fake user', async () => {
const expectedActions = [{
type: 'SEACH_REQUEST'
}, {
type: 'SEARCH_PHOTOS',
payload: []
}];
mock.onGet('/search/photos', {
params: {
query: 'cars',
page: 1
}
}).reply(200, []);
const data = await store.dispatch(searchPhotos(term));
const receivedActions = store.getActions();
expect(receivedActions).toEqual(expectedActions);
});
});