【问题标题】:Unit testing Solidity JS单元测试 Solidity JS
【发布时间】:2022-01-24 02:47:46
【问题描述】:

我想用 Javascript 在我的 mint 函数中测试这 3 个不同的需求。

function mint(uint256 _mintAmount) public payable {
    require(_mintAmount > 0, "need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "max mint amount per session exceeded");
    require(supply + _mintAmount <= maxSupply, "max NFT limit exceeded");

我对测试真的很陌生,所以我尝试了类似的方法。显然它是错误的,但我如何在这样的函数中测试 _mintAmount

it('mint amount', async () =>{
    _mintAmount > 0;
    _mintAmount <= maxMintAmount;
    supply + _mintAmount <= maxSupply;
})

【问题讨论】:

标签: javascript solidity truffle


【解决方案1】:

最终这样做了,

因为所有的支票都已经在我的 mint 函数中了。 mint(0) 将返回“需要至少铸造 1 个 NFT”,所以我为此使用了 assert.equal()。通过使用 try/catch 来获取还原错误消息,我检查了我想要的任何输入的预期输出。

it('mint amount', async function () {
    try {
        await NFT.mint.sendTransaction(0);
    }
    catch (err) {
        assert.equal("need to mint at least 1 NFT", err.reason);
    }
});

it('mint amount2', async function () {
    try {
        await NFT.mint.sendTransaction(1);
    }
    catch (err) {
        assert.equal("max mint amount per session exceeded", err.reason);
    }
});

it('mint amount3', async function () {
    try {
        await NFT.mint.sendTransaction(2);
    }
    catch (err) {
        assert.equal("max NFT limit exceeded", err.reason);
    }
});

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 2017-04-06
  • 2018-04-26
  • 1970-01-01
  • 2023-01-12
  • 2020-09-02
  • 2018-11-03
  • 1970-01-01
相关资源
最近更新 更多