0

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

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

4

1 回答 1

1

您的代码中有几个问题:

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

其次params您在方法中提供给 axios 模拟的属性与在您的操作创建onGet器中的操作中发送的参数不匹配。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);
    });
});
于 2020-04-17T17:02:49.557 回答