20 Foundry Test: 无效的 MockCall

智能合约代码:

function mintWhitelist() external {
        uint256 totalMinted = totalSupply();
        require(totalMinted < MAX, "All minted");

        uint256 whitelistAmount = whitelistNumber[msg.sender];
        require(whitelistAmount > 0, "Not in whitelist");
...
    }

测试代码:

    function test_mintWhitelist() public {
        vm.mockCall(
            address(addrContract),
            abi.encodeWithSelector(contract.totalSupply.selector),
            abi.encode(MAX)
        );
        assertEq(Contract.totalSupply(), MAX);

        vm.expectRevert("All minted");
        vm.prank(USER3);
        contract.mintWhitelist();
    }

测试结果:

[FAIL. Reason: Error != expected error: Not in whitelist != All minted] test_mintWhitelist_Part1() (gas: 18900)
Traces:
  [18900] ContractTest::test_mintWhitelist_Part1() 
    ├─ [0] VM::mockCall(Contract: [0x1a94a1Ae151C8e66Fb3a2E8843144969Bd2efc28], 0x18160ddd, 0x0000000000000000000000000000000000000000000000000000000000015b38) 
    │   └─ ← ()
    ├─ [0] Contract::totalSupply() [staticcall]
    │   └─ ← 10000 [1e4]
    ├─ [0] VM::expectRevert(All minted) 
    │   └─ ← ()
    ├─ [0] VM::prank(0x0000000000000000000000000000000000000003) 
    │   └─ ← ()
    ├─ [6811] Contract::mintWhitelist() 
    │   └─ ← "Not in whitelist"
    └─ ← "Error != expected error: Not in whitelist != All minted"

从 Trace 可以看到,totalSupply() 已经变成了 10000,可是为什么测试到 mint 函数的时候,没有报 All minted 错误呢?

请先 登录 后评论

最佳答案 2023-10-01 09:29

无人回答,取消提问

请先 登录 后评论

其它 2 个回答

Joe
请先 登录 后评论
Joe
请先 登录 后评论
  • 2 关注
  • 1 收藏,1849 浏览
  • NPC.李括 提出于 2023-09-16 22:49