forge-std icon indicating copy to clipboard operation
forge-std copied to clipboard

feat: `expectAndMockCall`

Open deluca-mike opened this issue 10 months ago • 0 comments

It would be nice to have this:

vm.expectAndMockCall(
    inboxes_[0],
    abi.encodeWithSelector(
        MockERC20Inbox.sendContractTransaction.selector,
        100_001,
        1_000_000,
        _appChainGateway,
        0,
        abi.encodeCall(IAppChainGatewayLike.receiveParameters, (0, keyChains_, values_))
    ),
    abi.encode(uint256(11))
);

instead of this:

vm.expectCall(
    inboxes_[0],
    abi.encodeWithSelector(
        MockERC20Inbox.sendContractTransaction.selector,
        100_000,
        1_000_000,
        _appChainGateway,
        0,
        abi.encodeCall(IAppChainGatewayLike.receiveParameters, (0, keyChains_, values_))
    )
);

vm.mockCall(
    inboxes_[0],
    abi.encodeWithSelector(
        MockERC20Inbox.sendContractTransaction.selector,
        100_001,
        1_000_000,
        _appChainGateway,
        0,
        abi.encodeCall(IAppChainGatewayLike.receiveParameters, (0, keyChains_, values_))
    ),
    abi.encode(uint256(11))
);

Sure, we could make a helper function:

function _expectAndMockCall(address callee, bytes memory data, bytes memory returnData) internal {
    vm.expectCall(callee, data);
    vm.mockCall(callee, data, returnData);
}

but forge-std is that helper tooling, so it probably makes sense that it lives there.

See this foundry issue.

deluca-mike avatar Apr 09 '25 19:04 deluca-mike