【发布时间】:2018-07-04 14:56:33
【问题描述】:
我正在尝试根据文档使用redux-mock-store
import React from 'react'
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import configureStore from 'redux-mock-store'
import App from '.'
import rootReducer from '../../store/reducers'
Enzyme.configure({ adapter: new Adapter() })
describe('The App container', () => {
let store
const mockStore = configureStore()
beforeEach(() => store = mockStore(rootReducer))
it('renders without crashing', () => {
const tree = Enzyme.shallow(<App store={store} />)
expect(tree).toMatchSnapshot()
})
})
问题是,我收到以下错误:
reducer 收到的先前状态是意外类型。预期参数是 Immutable.Collection 或 Immutable.Record 的实例,具有以下属性:
还有:
TypeError: inputState.withMutations 不是函数
我不应该包括rootReducer吗?
有没有更好的方法来初始化模拟商店?
编辑:我也试过用initialState来开店,但不行:
import React from 'react'
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import configureStore from 'redux-mock-store'
import App from '.'
Enzyme.configure({ adapter: new Adapter() })
describe('The App container', () => {
let store
const mockStore = configureStore()
const initialState = {}
beforeEach(() => store = mockStore(initialState))
it('renders without crashing', () => {
const tree = Enzyme.shallow(<App store={store} />)
expect(tree).toMatchSnapshot()
})
})
● App 容器 › 渲染时不会崩溃
TypeError: state.get 不是函数
【问题讨论】:
-
查看文档,您不应该传入 initialState 而不是 rootReducer 吗?还是某种物体?
-
是的,当我尝试使用 initialState 时,我得到了
store.getState is not a function
标签: javascript unit-testing redux mocking redux-mock-store