【发布时间】:2018-04-23 16:10:31
【问题描述】:
假设我有一个这样的组件:
class ExampleComponent extends Component {
exampleMethod = () => {
this.props.examplePropFunction()
}
}
然后我像这样构建我的测试:
it('exampleMethod', () => {
const examplePropMock = jest.fn()
const wrapper = shallow(<ExampleComponent examplePropFunction={examplePropMock}/>)
wrapper.instance().exampleMethod()
expect(examplePropMock).toHaveBeenCalled()
})
失败了
TypeError: _this.props.examplePropFunction is not a function
console.logging 它显示它实际上是未定义的。
一个简单的解决方案是运行:
wrapper.setProps({examplePropFunction: examplePropMock})
在获取实例之前。我的问题是,为什么最后一步是必要的?
【问题讨论】:
标签: reactjs testing jestjs enzyme