mul*_*dy1 5 sinon reactjs ecmascript-next
我终于将我的组件更新React.createClass()为ES6类.我在测试中看到的第一个错误是我的一些sinon.stub()调用失败,因为jscodeshift -t react-codemod/transforms/class.js转换了我的组件方法以使用属性初始化程序提议,即
class Foo extends React.Component {
componentWillMount() {
this.fetchSomeData(this.props);
}
componentWillReceiveProps(nextProps) {
if (nextProps.someProp !== this.props.someProp) {
this.fetchSomeData(nextProps);
}
}
fetchSomeData = (props) => {
...
};
render() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何fetchSomeData()使用这种新语法存根?我的测试看起来sinon.stub(Foo.prototype, 'fetchSomeData');不再适用,假设因为不再fetchSomeData是原型.
谢谢!
在这个例子中,fetchSomeData()实际上确实附加到this而不是附加到Foo.prototype,因此在创建实例之前绝对没有办法对方法进行存根。解决方法是将逻辑移至可以fetchSomeData()存根的不同位置。或者使用不同的语法来定义方法。