Chai - 断言错误:预期 BigNumber

问题描述 投票:0回答:2

我尝试测试这段代码:

contract Token {
    // Some string type variables to identify the token.
    string public name = "My Hardhat Token";
    string public symbol = "MHT";

    // The fixed amount of tokens, stored in an unsigned integer type variable.
    uint256 public totalSupply = 1000;

    // An address type variable is used to store ethereum accounts.
    address public owner;

    // A mapping is a key/value map. Here we store each account's balance.
    mapping(address => uint256) balances;

    // The Transfer event helps off-chain applications understand
    // what happens within your contract.
    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    /**
     * Contract initialization.
     */
    constructor() {
        // The totalSupply is assigned to the transaction sender, which is the
        // account that is deploying the contract.
        balances[msg.sender] = totalSupply;
        owner = msg.sender;
    }

    /**
     * A function to transfer tokens.
     *
     * The `external` modifier makes a function *only* callable from *outside*
     * the contract.
     */
    function transfer(address to, uint256 amount) external {
        // Check if the transaction sender has enough tokens.
        // If `require`'s first argument evaluates to `false` then the
        // transaction will revert.
        require(balances[msg.sender] >= amount, "Not enough tokens");

        // Transfer the amount.
        balances[msg.sender] -= amount;
        balances[to] += amount;

        // Notify off-chain applications of the transfer.
        emit Transfer(msg.sender, to, amount);
    }

    /**
     * Read only function to retrieve the token balance of a given account.
     *
     * The `view` modifier indicates that it doesn't modify the contract's
     * state, which allows us to call it without executing a transaction.
     */
    function balanceOf(address account) external view returns (uint256) {
        return balances[account];
    }
}

我使用这个js代码

    const { expect } = require("chai");

describe("Token contract", function () {
  it("Deployment should assign the total supply of tokens to the owner", async function () {
    const [owner] = await ethers.getSigners();

    const Token = await ethers.getContractFactory("Token");

    const hardhatToken = await Token.deploy();

    const ownerBalance = await hardhatToken.balanceOf(owner.address);
    expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
  });
});

错误是:

AssertionError: expected BigNumber { value: "1000" } to equal BigNumber { value: "1000" }
javascript solidity chai
2个回答
1
投票

似乎对我来说效果很好。尝试运行

npx hardhat clean
,然后重试。


0
投票

遇到同样的错误。 所有者余额: BigNumber { value: "10000" } 总供应量:BigNumber {值:“10000”} 1) 部署应将代币的总供应量分配给所有者

0 通过(649ms) 1 次失败

  1. 代币合约 部署应将代币的总供应量分配给所有者:

    断言错误:预期 BigNumber{ _hex: '0x2710', ...(1) } 等于 BigNumber{ _hex: '0x2710', ...(1) }

    • 预期 - 实际

    在上下文中。 (文件:///Users/sundrammishra/Desktop/BlockChain/hardhat/test/token.js:23:28)

© www.soinside.com 2019 - 2024. All rights reserved.