简体   繁体   English

React - sinon 测试 - 获取 TypeError:无法读取未定义的属性“子字符串”

[英]React - sinon testing - getting TypeError: Cannot read property 'substring' of undefined

I want to test if the function destructures the given arguments properly.我想测试该函数是否正确地解构了给定的参数。 The function is:功能是:

function testFunc(...args) {
  const [nameOfClass, itemName] = args;

  return <div className={nameOfClass}>{itemName}</div>
}

I am using mocha, chai and sinon to test if arguments provided to that function are properly destructured.我正在使用 mocha、chai 和 sinon 来测试提供给该函数的参数是否正确解构。 However the test give me "AssertionError: expected false to be true":但是测试给了我“AssertionError:预计假为真”:

const funcArguments = ['testClass', 'testItemName'];
const spy = sinon.spy();
testFunc(...funcArguments, spy);
expect(spy.calledWithExactly('testClass', 'testItemName')).to.be.true

Is it the fault of destructuring?是解构的错吗?

Thanks to the input in the comments, I adjusted the test code to actually call the spy.感谢评论中的输入,我调整了测试代码以实际调用spy。 It looks like it now and it gives the proper result:现在看起来像这样,它给出了正确的结果:

const funcArguments = ['testClass', 'testItemName'];
const spiedTestFunc = sinon.spy(testFunc);
spiedTestFunc(...funcArguments);
expect(spiedTestFunc.calledWithExactly('testClass', 'testItemName')).to.be.true

暂无
暂无

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

相关问题 TypeError:无法读取反应中未定义的属性“子字符串” - TypeError: Cannot read property 'substring' of undefined in react 反应单元测试:TypeError:无法读取未定义的属性“ test” - React Unit Testing: TypeError: Cannot read property 'test' of undefined React - 使用 Jest 进行测试 - TypeError:无法读取未定义的属性“销毁” - React - Testing w/ Jest - TypeError: Cannot read property 'destroy' of undefined React 测试库:TypeError:无法读取未定义的属性“文章” - React Testing library: TypeError: Cannot read property 'articles' of undefined 类型错误:无法在反应/redux 测试中读取未定义的属性“路径名” - TypeError: Cannot read property 'pathname' of undefined in react/redux testing 为什么我得到“TypeError:无法读取未定义的属性&#39;恢复&#39;当使用sinon.js删除mongoose时 - Why Am I getting “TypeError: Cannot read property 'restore' of undefined” when stubbing mongoose with sinon.js 反应表获取问题类型错误:无法读取未定义的属性“forEach” - React table getting issue TypeError: Cannot read property 'forEach' of undefined ×获取TypeError:无法读取未定义的属性“ map” - × Getting a TypeError: Cannot read property 'map' of undefined 得到这个类型错误:无法读取未定义的属性“地图” - getting this TypeError: Cannot read property 'map' of undefined React - TypeError:无法读取未定义的属性“setState” - React - TypeError: Cannot read property 'setState' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM