![](/img/trans.png)
[英]TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
[英]Why is this is undefined when mocking event in React unit testing, using sinon.spy to mock function and enzyme.shallow render for React Component?
每当我尝试模拟在那里我传递一个事件, event
对象,我得到的是this
是undefined
。
const onBlur = sinon.spy(Input.prototype.handleBlur);
const wrapper = shallow(<Input handleBlur={onBlur} />);
//...
wrapper.find('input').simulate('blur', { target: {value: ''} });
在handleBlur
方法内部, this
是未定义的。 但是,如果我决定不模拟event
对象,则事件未定义。
另外,我尝试使用mount
代替: const wrapper = shallow(<Input handleBlur={onBlur} />);
但是,这是同一回事。 但是,直到更新npm库,我什至无法理解这一点。
尝试使用
const onBlur = sinon.spy(Input.prototype, "handleBlur");
代替
const onBlur = sinon.spy(Input.prototype.handleBlur);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.