Solidity – Writing Foundry Tests for Functions with Arrays of Structs

foundrysoliditytesting

I have this foundry function and I don't have any idea how to pass an array of structs to batchList function

function test_ListMockERC721() public {
    vm.prank(add1);
    erc721Mock.mint(msg.sender);
    marketsc.batchList([[erc721Mock, 1, 0, add1, 1], [erc721Mock, 2, 0, add1, 1]]);
}

This is the function signature from the smart contract:

function batchList(InputOrder[] calldata inputOrders) external;

Best Answer

I assume that InputOrder is a structure. Then the solution is:

function test_ListMockERC721() public {
    vm.prank(add1);
    erc721Mock.mint(msg.sender);
    marketsc.InputOrder[] memory inputOtders = new marketsc.InputOrder[](2);
    inputOtders[0] = marketsc.InputOrder(erc721Mock, 1, 0, add1, 1);
    inputOtders[1] = marketsc.InputOrder(erc721Mock, 2, 0, add1, 1);
    marketsc.batchList(inputOtders);
}
Related Topic