【问题标题】:How to make chai test passing?如何使柴测试通过?
【发布时间】: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


    【解决方案1】:

    在 chai api 上没有text 的断言。

    你应该做一些类似的事情-

    expect('foobar').to.contain('foo');

    【讨论】:

    • 问题出在您的render 函数上。将{this.props.myprops.data.text} 编辑为{this.props.data.text}
    猜你喜欢
    • 2019-08-26
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多