简体   繁体   English

如何使用摩卡茶和酶对道具的状态进行单元测试?

[英]How to unit test the state of props using mocha chai and enzyme?

I'm trying to unit test onClick the state of the props in my component clear. 我正在尝试对组件清晰显示中的道具状态进行onClick单元测试。

I tried doing it this way: 我尝试这样做:

props = {
    attributeTableData: data,
    clearMessage: onClickMethod,
    reset: () => { },
    resetAttributeTable: () => { },
    statusMessage: {
        messageType: 'message-success',
        userMessage: 'Template has been saved successfully. Please wait …see your results display with the latest'
    },
    submitTemplateCreationStatus: () => { },
    templateAttributeFormData: () => { },
    templateFormSubmission: true,
    templateAttributeFormSubmission: true,
    templateFormData: () => { },
    userRoles: new Set(['admin'])
};
let emptyStatusMessage = {};
actualComponent = shallow(<CreateTemplateResults { ...props } />);
actualComponent.instance().resetForms();
expect(onClickMethod.called).to.be.true;
expect(actualComponent.state('statusMessage')).to.eql(emptyStatusMessage)

But I get: 但我得到:

" TypeError: ShallowWrapper::state("statusMessage") requires that state not be null or undefined " “ TypeError:ShallowWrapper :: state(” statusMessage“)要求state不能为nullundefined

You are creating a shallow render of <CreateTemplateResults /> , but never passing actualComponent.setState(nextState) . 您正在创建<CreateTemplateResults />的浅表呈现器,但从未传递actualComponent.setState(nextState) Therefore, when you are trying to access the state on your last line, it is throwing an error because state is null / undefined . 因此,当您尝试访问最后一行的状态时,由于state为null / undefined ,因此将引发错误。

shallow().setState 浅()。的setState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用业力摩卡/柴酶对基本三元进行单元测试? - how to unit test a basic ternary using karma-mocha/chai-enzyme? 如何使用酶,摩卡,柴对单元自定义的React函数进行单元测试 - How do you unit test a custom React function using enzyme, mocha, chai 使用standardJS在Mocha Chai单元测试中没有未使用的表达式 - No unused expressions in mocha chai unit test using standardJS 如果变量具有反应组件作为值,我如何使用 mocha/chai/sinon 进行单元测试? - How do i unit test using mocha/chai/sinon if a variable has a react component as value? 如何编写用于使用mongoose,mocha和chai将对象插入和检索到mongodb的单元测试? - How to write a Unit test for inserting and retrieving an object to mongodb using mongoose, mocha and chai? 如何为chai Expect提供用于摩卡单元测试的自定义错误消息? - How to provide chai expect with custom error message for mocha unit test? 如何对路由器功能NodeJS / Mocha / Chai进行单元测试 - How to carry unit test on router function NodeJS / Mocha / Chai 使用 Chai 和 mocha 的测试用例 - Test cases using Chai and mocha 你如何测试酶/摩卡/柴中div或其他元素的含量? - How do you test the content of a div or other element in enzyme/mocha/chai? 如何使用Mocha Chai组织单元测试BDD的代码? - How to organize code for unit testing BDD using Mocha Chai?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM