【问题标题】:Non-deterministic exceptions when running truffle tests运行松露测试时的非确定性异常
【发布时间】:2017-11-28 11:39:38
【问题描述】:

我一直在使用他们的测试服务(我相信它使用 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 并且该测试通过。

每次测试前,我退出 truffle 控制台,从项目中删除我的构建文件夹,重新进入 truffle 控制台并测试(这样我可以假设链已重置),但我仍然收到错误消息首先,没有意义,因为我在通过 Dapp 自己测试时没有得到这些结果,其次,不要每次都出现,这似乎是随机时间和随机错误。

还有其他人遇到过这个问题吗?

【问题讨论】:

    标签: testing mocha.js solidity truffle consensys-truffle


    【解决方案1】:
    • 这是令人困惑的,因为断言与错误消息不匹配 - 但是更改断言失败,返回的消息是通过的早期测试的一部分(截止日期在受益人点击开始时设置)。

    这是非常典型的 javaScript 异步测试,无论您是在进行 ethereum truffle 测试还是只是从前端进行简单的 Karma-Jasmine 测试。

    我非常有信心您在某些规范中的回调中有断言,但未正确设置。由于缺少调用 'done' 回调参数,或者因为您没有在 it 调用中正确返回 Promise,因此规范通过,因此测试只是作为误报通过,忽略回调内容。

    一段时间后,回调中的代码运行并失败,从而使正在运行的测试失败,但给出了来自其测试的错误消息,并给出了错误的否定 - 因为您在错误的测试中遇到了异常,从而使其失败。

    这也可能是由.catch 语句吞下done 调用引起的。

    当然,也许我错了,您的问题与Truffle 本身或testrpc 确实有关。

    如果您绝对确定您的所有 it 规范都正确处理异步 - 返回 Promise 或调用 done-,那么您应该粘贴您的测试代码并等待更多反馈:-S

    另外,您是否在测试期间使用 testrpc 作为您的区块链?

    【讨论】:

    • 我的印象是你应该在测试中使用回调或承诺,而不是像这里的 mocha 文档 mochajs.org/#hooks 所引用的那样同时使用。因此,我没有在任何地方实现 done() ,因为我的 .then() 函数应该等到返回承诺,不是吗?但是,这对于为什么断言错误会给出先前测试的错误确实完全有道理,但即便如此,它们不会每次都返回相同的失败吗?
    • 是的,testrpc 现在自动集成到 truffle 开发控制台中,如下所述:github.com/trufflesuite/ganache-cli
    • 我在谈论使用 done() 或返回承诺,因为我不知道你的选择是什么。只有返回承诺是完全可以的。
    • 我猜你的问题隐藏在任何不正确的returned 承诺中,无论是来自it 规范本身还是来自任何thenable。刚刚过去你的测试代码,让我们看看
    • 我的错误,我以为我已将测试代码添加到问题中!我现在就这样做,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-09-22
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2012-12-02
    • 1970-01-01
    相关资源
    最近更新 更多