【发布时间】:2016-10-29 06:28:05
【问题描述】:
我已经为 reactjs 应用编写了我的第一个单元测试:
it('should display props.text', () => {
const props = {
data: {
completed: false,
id: 1,
text: 'hey boer'
}
};
const wrapper = mount(<SubComponent {...props} />);
const part = wrapper.find('.mystuff');
//try to see whether the mockdata is present
expect(part).to.contain('hey boer');
});
这是我的组件:
import React, {Component, PropTypes} from 'react';
export class SubComponent extends React.Component {
constructor(props) {
console.log('cons servicedetails props', props);
super(props);
}
render() {
return (
<div className="mystuff">this is the subcomponent. {this.props.myprops.data.text}</div>
)
}
}
export default SubComponent;
当我运行测试时,我得到了这个错误:
SubComponent should display props.text:
TypeError: Cannot read property 'data' of undefined
如何检查我的 props(mock) 数据是否实际注入到子组件中?我的程序运行没有错误但单元测试抛出错误?另见here
【问题讨论】:
标签: unit-testing reactjs react-redux chai