簡體   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