Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/480.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用moxios测试redux异步方法_Javascript_Redux_Axios - Fatal编程技术网

Javascript 用moxios测试redux异步方法

Javascript 用moxios测试redux异步方法,javascript,redux,axios,Javascript,Redux,Axios,我是redux的新手,我会把头发拔出来,试着用redux和moxios做一个基本的测试 API只是axios,设置了一些自定义头 我的post方法出现错误: TypeError:无法读取未定义的属性“then” 我的方法: const login = ({username, password}) => (dispatch) => { dispatch(actions.loginRequested()); return API.post(`curavi/v2/authentic

我是redux的新手,我会把头发拔出来,试着用redux和moxios做一个基本的测试

API只是axios,设置了一些自定义头

我的post方法出现错误: TypeError:无法读取未定义的属性“then”

我的方法:

const login = ({username, password}) => (dispatch) => {
  dispatch(actions.loginRequested());
  return API.post(`curavi/v2/authentication`, {username, password})
    .then(response => dispatch(actions.loginSuccess(response.data.payload)))
    .catch((error) => errorHandler(dispatch, error.response));
};
我的测试用例:

describe('login', () => {

  beforeEach(function () {
    // import and pass your custom axios instance to this method
    moxios.install(API)
  });

  afterEach(function () {
    // import and pass your custom axios instance to this method
    moxios.uninstall(API)
  });

  test('calls loginSuccess when the response is successful', () => {
    const store = mockStore();

    const mockData = {
      data: { payload: 'yay' }
    };

    moxios.wait(() => {
      const request = API.requests.mostRecent();
      request.respondWith({
        status: 200,
        response: mockData
      });
    });

    const expectededActions = [
      {type: types.LOGIN_REQUESTED},
      {type: types.LOGIN_SUCCESS, payload: 'yay'}
    ];

    actions.loginRequested.mockReturnValue({type: types.LOGIN_REQUESTED});
    actions.loginSuccess.mockReturnValue({type: types.LOGIN_SUCCESS, payload: 'yay'});
    actions.loginFail.mockReturnValue({type: types.LOGIN_FAIL, message: 'boo'});

    return store.dispatch(operations.login({username: 'theuser', password: 'thepassword'}))
      .then(() => {
        expect(store.getActions()).toEqual(expectededActions);

        expect(API.post).toHaveBeenCalledWith('curavi/v2/authentication',
          {username: 'theuser', password: 'thepassword'});
      });
  })
});

您确定按照您的建议在
login
中出现
TypeError
?这没有意义;如果
API
不是axios实例,则会出现该错误,在这种情况下
API.post()
可能返回未定义。另一方面,由于以下两个原因,您的测试不起作用:

  • 您需要将
    API.requests.mostRecent()
    替换为
    moxios.requests.mostRecent()

  • moxios'Wait中的函数在0.5秒内不会执行,请参阅。如果测试中的返回语句在此之前到达,那么测试只会返回一个承诺。您可以改为执行以下操作:

    test('...', async () => {
    
        // ...
    
        const result = await store.dispatch(
            operations.login({
                username: 'theuser',
                password: 'thepassword',
            })
        );
    
        expect(store.getActions()).toEqual(expectededActions);
        expect(API.post).toHaveBeenCalledWith(/* ... */);
    });
    
您还应确保正确设置存储:

import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';

const middlewares = [thunk];
const mockStore = configureStore(middlewares);

// use your store inside your tests
const store = mockStore();

谢谢,原来根本原因有点傻。我试图从本教程中的模块中设置moxios:然后就忘了它。一旦我删除了它并像你说的那样更改了API.requests.mostRecent(),一切都开始工作了。谢谢