【问题标题】:Chai should be bignumber equal doesn't work柴应该是大数相等不起作用
【发布时间】:2022-07-27 03:00:01
【问题描述】:

我需要这样的柴

const BigNumber = web3.BigNumber;
require('chai').use(require('chai-bignumber')(BigNumber)).should();

在测试期间

let balance = await contract.balanceOf(accountToReceive);
should.be.bignumber.eql(countToSend); //works fine
ownerBalance = await contract.balanceOf(owner);
should.be.bignumber.equal(settings.initialSupply.mul(utils.toBN(10).pow(_decimals)).sub(countToSend)); // gives en error

错误码是

AssertionError: expected <BN: 94e47b8d68171533ffff9c> to equal <BN: 94e47b8d68171533ffff9c>
     + expected - actual

     at Context.<anonymous> (test/MOS.test.js:79:33)
     at processTicksAndRejections (node:internal/process/task_queues:96:5)

【问题讨论】:

  • 出现问题出在 .sub 中,但我仍然无法修复它。
  • 你为什么使用equal?它们将是不同的 BigNumber 对象,不是吗?我认为您应该使用eql。请记住,expect({ a: 1 }).to.equal({ a: 1 }); 失败是因为它们不是同一个对象。但是expect({ b: 2 }).to.eql({ b: 2 }); 通过了,因为它进行了深度比较。
  • 我得到同样的错误,只是在 equal 的地方有 deep equal AssertionError: expected &lt;BN: 94e47b8d68171533ffff9c&gt; to deep equal &lt;BN: 94e47b8d68171533ffff9c&gt;
  • 更新:问题出在 .add 和 .sub 中。当我将 BN 转换为数字时,添加它们然后将它们转换回 BN 它将起作用。但是有些数字太大了,无法双向转换为 Number 类型
  • 更新:我已经解决了使用 BigInt 的问题,使用 BigInt 对象进行所有数学运算,然后使用 utils.toBn(someBigIntNumber.toString()) 但我认为应该有更好的解决方案

标签: testing solidity chai


【解决方案1】:

我的代码也遇到了同样的问题。我找到了一个适合我的好解决方案。

尝试以下方法:

require('chai').use(require('chai-as-promised')).should();
.use(bnChai(web3.utils.BN));

【讨论】:

  • 感谢您的反应,但没有帮助
【解决方案2】:

正如我在更新中提到的,你可以使用 JS Bigint,用它做所有的数学运算,然后将它作为字符串传递给 BN 构造函数,比如 utils.toBN((1000000n * 5748553687688487n).toString())

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,答案很简单 只需将 chai 的 expect 函数与 to.be.bignumber.equal 一起使用即可

    示例:

    const { expect } = chai;
    const { BN } = require('@openzeppelin/test-helpers');
    
    const exp_val = new BN(); // any value
    expect(contract.method()).to.be.bignumber.equal(exp_value);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 2016-04-24
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多