在 React 组件的基本渲染测试中,哪种方法更可靠?
const wrapper = shallow(<MyComponent />);
expect(wrapper.exists()).to.equal(true);
expect(wrapper.find('div').length).to.equal(1);
测试渲染的子组件也是如此:
const wrapper = shallow(<MyComponent />);
expect(wrapper.find(MyChildComponent).exists()).to.equal(true);
expect(wrapper.find(MyChildComponent).length).to.equal(1);
我发现 exists()
更加惯用。但我发现大多数人在测试中使用 length
版本。如果有的话,有哪些权衡?
最佳答案
exists()
只是一个糖 wrapper长度。
exists() {
return this.length > 0;
}
所以使用哪一个取决于你,像你一样,我认为 exists()
更具描述性,更喜欢使用它。
关于unit-testing - 基本 react 渲染测试: exists() vs.长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888943/