[英]Non-deterministic exceptions when running truffle tests
我一直在使用他們的測試服務(我相信使用Mocha和Chai)編寫自動化測試來測試我在Truffle中的眾籌功能。 我很難理解為什么測試會不確定性地失敗(即,它們似乎在隨機點失敗,但有隨機例外)。
例如,我運行測試,有時它們都通過了
我運行測試,有時會收到以下錯誤消息:
✓ deadline is set when beneficiary clicks start
✓ should return a State of 'Funding' after start is clicked (101ms)
✓ should allow someone to invest and record balance
✓ should send 1 ERC20 token to participant (184ms)
✓ softcap should not return reached when amountRaised is smaller
✓ softcap should return reached when amountRaised is bigger
✓ hardcap should not return reached when amountRaised is smaller
✓ hardcap should return reached when amountRaised is bigger
1) should give 1 ETH in change as hard cap was exceeded
> No events were emitted
✓ should return a State of 'Successful' when hardcap reached
✓ beneficiary should be able to withdraw funds
✓ should return a State of 'Finished' when beneficiary has funds
22 passing (2s)
1 failing
1) Contract: Sale should give 1 ETH in change as hard cap was exceeded:
Uncaught AssertionError: deadline was not set after start pressed by beneficiary: expected '0' to not equal 0
at test/2Crowdsale_Test.js:119:14
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
1
truffle(develop)>
/usr/local/lib/node_modules/truffle/build/cli.bundled.js:320098
throw reason;
^
TypeError: Cannot read property 'currentRetry' of undefined
at
/usr/local/lib/node_modules/truffle/node_modules/mocha/lib/runner.js:
552:28
at done
(/usr/local/lib/node_modules/truffle/node_modules/mocha/lib/runnable.js
:295:5)
at
/usr/local/lib/node_modules/truffle/node_modules/mocha/lib/runnable.js:
359:11
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
這很容易混淆,因為斷言與錯誤消息不匹配-更改斷言失敗,但是,返回的消息是通過的較早測試的一部分(受益人單擊開始時設置了截止日期)。
再次運行測試,我得到:
✓ approves contract by beneficiary
✓ should return a State of 'Not Started' before start is clicked (134ms)
✓ deadline is set when beneficiary clicks start
✓ should return a State of 'Funding' after start is clicked (98ms)
✓ should allow someone to invest and record balance
✓ should send 1 ERC20 token to participant (143ms)
✓ softcap should not return reached when amountRaised is smaller
✓ softcap should return reached when amountRaised is bigger
✓ hardcap should not return reached when amountRaised is smaller
✓ hardcap should return reached when amountRaised is bigger
1) should give 1 ETH in change as hard cap was exceeded
> No events were emitted
2) should return a State of 'Successful' when hardcap reached
Events emitted during test:
---------------------------
Transfer(_from: <indexed>, _to: <indexed>, _value: 5)
FundTransfer(backer: 0xf17f52151ebef6c7334fad080c5704d77216b732, amount: 5000000000000000000, isContribution: true)
reachedSoftCap(recipient: 0x627306090abab3a6e1400e9345bc60c78a8bef57, totalAmountRaised: 6000000000000000000)
---------------------------
✓ beneficiary should be able to withdraw funds
✓ should return a State of 'Finished' when beneficiary has funds
21 passing (2s)
2 failing
1) Contract: Sale should give 1 ETH in change as hard cap was exceeded:
Uncaught AssertionError: deadline was not set after start pressed by beneficiary: expected '0' to not equal 0
at test/2Crowdsale_Test.js:119:14
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
2) Contract: Sale should return a State of 'Successful' when hardcap reached:
Uncaught AssertionError: hardcap was not met when amountRaised was bigger: expected false to equal true
at test/2Crowdsale_Test.js:195:14
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
2
truffle(develop)>
/usr/local/lib/node_modules/truffle/build/cli.bundled.js:320098
throw reason;
^
TypeError: Cannot set property 'state' of undefined
at
/usr/local/lib/node_modules/truffle/node_modules/mocha/lib/runner.js:
576:20
at done
(/usr/local/lib/node_modules/truffle/node_modules/mocha/lib/runnable.js
:295:5)
at /usr/local/lib/node_modules/truffle/node_modules/mocha/lib/runnable.js:353:11
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
現在,我有了上一條錯誤消息,現在又出現了另一條錯誤消息,該錯誤消息與斷言錯誤不匹配。
我第四次運行測試:
✓ should allow someone to invest and record balance
✓ should send 1 ERC20 token to participant (120ms)
✓ softcap should not return reached when amountRaised is smaller
✓ softcap should return reached when amountRaised is bigger
✓ hardcap should not return reached when amountRaised is smaller
✓ hardcap should return reached when amountRaised is bigger
✓ should give 1 ETH in change as hard cap was exceeded (275ms)
✓ should return a State of 'Successful' when hardcap reached (61ms)
✓ beneficiary should be able to withdraw funds
1) should return a State of 'Finished' when beneficiary has funds
> No events were emitted
22 passing (2s)
1 failing
1) Contract: Sale should return a State of 'Finished' when beneficiary has funds:
AssertionError: State of contract was not 'Finished': expected '4' to equal 6
at test/2Crowdsale_Test.js:243:14
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
與前兩個錯誤完全不同。 由於先前的測試(受益人應該能夠提取資金)也使狀態等於6的測試也通過了此測試,因此這又一次令人困惑。
每次測試前,我退出松露控制台,從項目中刪除我的構建文件夾,重新進入松露控制台並進行測試(這樣我就可以假定鏈已被重置),但是我仍然收到錯誤,首先我沒有通過Dapp對其進行測試時沒有得到這些結果,這是沒有道理的;其次,每次都不會顯示出來,這似乎是隨機的,帶有隨機的錯誤。
其他人對此有麻煩嗎?
這很容易混淆,因為斷言與錯誤消息不匹配-更改斷言失敗,但是,返回的消息是通過的較早測試的一部分(受益人單擊開始時設置了截止日期)。
這是javaScript異步測試的非常典型的結果,無論您是進行以太坊松露測試還是只是前端的純Karma-Jasmine測試。
我非常有信心在某些規范未正確設置的回調中包含斷言。 規范通過是由於缺少調用'done'回調參數的原因,或者是因為您沒有在it
調用內正確返回Promise,因此測試只是作為誤報通過,忽略了回調內容。
一段時間后,回調中的代碼將運行並失敗,從而使正在運行的測試失敗,但會給出來自其測試的錯誤消息以及錯誤的否定值-因為您在錯誤的測試中遇到了異常,因此它失敗了。
這也可能由.catch
語句大量記錄已done
調用引起。
當然,也許我錯了,而您的問題與Truffle
本身或testrpc
。
如果您是絕對確保所有it
的規格得以妥善處理異步返回-或者一個承諾或打電話done
- ,那么你應該粘貼測試代碼,等待更多的反饋:-S
另外,您在測試期間是否將testrpc用作區塊鏈?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.