我正在尝试使用酶和摩卡测试一个React组件,如下所示
import { mount, shallow } from 'enzyme';
import React from 'react';
import chai, { expect } from 'chai'
import chaiEnzyme from 'chai-enzyme'
import sinon from 'sinon'
import MyComponent from 'myComponent'
chai.use(chaiEnzyme())
describe('MyComponent', () => {
const store = {
id: 1
}
it ('renders', () => {
const wrapper = mount(<MyComponent />, {context: {store: store}})
})
})我还没有实际编写测试,因为它在wrapper的声明中失败
错误消息: TypeError:_this.store.getState不是函数
不知道问题是什么,也找不到任何解决这个问题的方法!
任何帮助都是最好的!
发布于 2017-12-06 17:06:10
此错误意味着存储无法正确获取状态。我建议使用redux-mock-store和导入configureStore模拟商店
import configureStore from 'redux-mock-store';然后通过这样做来模拟状态
const initialState = { id: 1 };
const mockStore = configureStore();您可以继续使用provider包装您的组件
import { Provider } from 'react-redux'; // add this to the top of your file
const wrapper = mount(
<Provider store={mockStore(initialState)}>
<MyComponent />
</Provider>,
);发布于 2017-01-28 01:01:18
另外,在代码示例中,chai.user()不应该是chai.use()吗?
https://stackoverflow.com/questions/41899128
复制相似问题