繁体   English   中英

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

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

我想测试该函数是否正确地解构了给定的参数。 功能是:

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

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

我正在使用 mocha、chai 和 sinon 来测试提供给该函数的参数是否正确解构。 但是测试给了我“AssertionError:预计假为真”:

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

是解构的错吗?

感谢评论中的输入,我调整了测试代码以实际调用spy。 现在看起来像这样,它给出了正确的结果:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM