【问题标题】:axios-mock-adapter onGet mock data not effectiveaxios-mock-adapter onGet 模拟数据无效
【发布时间】:2020-07-30 06:37:05
【问题描述】:

我正在尝试使用 axios-mock-adapter 测试 axios 调用。我遇到了以下问题: 来自测试的 API 调用总是响应真实数据,而不是我用 mock.onGet 模拟的数据。 又名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);
      });
  });
});

任何人都遇到过类似的问题,或者可以给我一些建议。 提前致谢。

【问题讨论】:

    标签: reactjs unit-testing jestjs axios axios-mock-adapter


    【解决方案1】:

    您的代码中有几个问题:

    首先,在动作创建器中,您使用 axios 实例进行 ajax 调用,但在测试中您没有将该实例提供给 axios-mock-adapter。当您创建MockAdapter 的实例时,您应该在测试中提供您的 axios 实例。

    第二,您在onGet 方法中提供给axios mock 的params 属性与您的动作创建器中get 操作中发送的参数不匹配。您应该将调用中的参数与其值相匹配。因此,您应该提供querypage 参数。

    最后,您在模拟请求中返回 expectedActions,但这似乎不正确。查看您的代码,您似乎想返回一个空数组。

    考虑到所有这些,您的代码将如下所示:

    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);
        });
    });
    

    【讨论】:

    • 感谢您的回答和漂亮的代码。它正在工作!
    • 但是,receivedActions 中的有效负载是未定义的,如下所示: `` [{ type: 'SEACH_REQUEST' }, { type: 'SEARCH_PHOTOS', payload: undefined }] ``` I'我假设它应该是[](空数组),有什么想法吗?谢谢!
    • 未定义的问题似乎不是由您的代码引起的,它现在运行良好!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 2019-09-06
    • 2018-04-07
    • 2018-03-17
    • 1970-01-01
    • 2018-12-18
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多