我目前有一个像这样的组件:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getDataAction } from ' './my-component';

export class MyComponent extends { Component } {
   componentWillMount() {
      this.props.getData();
   }
   render(){

      <div>
      this.props.title
     </div>

   }
}

const mapStateToProps = (state) => ({
   title: state.title
});

const mapDispatchToProps = (dispatch) ({
   getData() {
      dispatch(getDataAction());
   }
});

export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)

而我正试图用jest和酶进行浅层渲染测试 .

测试:

import React from 'react';
import { shallow } from 'enzyme';
import { MyComponent } from './index';

it('renders without crashing', () => {
  shallow(<MyComponent getData={jest.fn()} />);
});

我的问题是,这是传统的模拟方式吗? Jest官方文档没有特别提到 Mock 道具,这篇文章是关于完全安装的测试 . 有没有其他方法来模拟dispatchToProps?在这个例子中只有一个,但是如果我在dispatchToProps中有很多函数怎么办?

问题:在我的真实文件中,我有一个像 this.props.information.value 这样的值的引用,我希望抛出像 cannot get value of undefined 这样的错误,因为信息没有被模拟/定义,但只有当函数不存在时才会抛出错误't. It' .