Reactjs 开玩笑地模仿`getAppState()`;酶

Reactjs 开玩笑地模仿`getAppState()`;酶,reactjs,unit-testing,redux,jestjs,enzyme,Reactjs,Unit Testing,Redux,Jestjs,Enzyme,我试图在React/Redux应用程序中测试我的操作,在我的一个操作中,我使用getAppState()获取Redux的当前状态。该操作没有参数,只需从返回的getAppState() actions/myFile/index.test.js 操作/myFile/index.js 当试图用Jest测试我的操作时,我得到一个错误,指出: TypeError: Cannot read property 'reducerProp' of undefined 我的问题是如何模拟getAppState(

我试图在React/Redux应用程序中测试我的操作,在我的一个操作中,我使用
getAppState()
获取Redux的当前状态。该操作没有参数,只需从返回的
getAppState()

actions/myFile/index.test.js

操作/myFile/index.js

当试图用Jest测试我的操作时,我得到一个错误,指出:

TypeError: Cannot read property 'reducerProp' of undefined
我的问题是如何模拟
getAppState()
func返回的内容并在测试中使用它们?我在谷歌上搜索过一次,看到了结果,我想,为什么不去StackOverflow问问自己lol

(我还假设你正在使用redux thunk)

您需要配置
存储

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

const initialState = {
  myReducer: { reducerProp: 'something here' }
};

const store = mockStore(initialState)

// Your code down here...

有关如何使用存储的更多信息,请参阅。

作为旁注:虽然可以为函数参数指定任何名称,但该参数通常称为
getState
,而不是
getAppState
。我一开始对你的意思很困惑,因为你用的是一个不熟悉的名字。对!很抱歉:/
TypeError: Cannot read property 'reducerProp' of undefined
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

const initialState = {
  myReducer: { reducerProp: 'something here' }
};

const store = mockStore(initialState)

// Your code down here...