mockCalls
Signature
Section titled “Signature”function mockCalls( address where, bytes calldata data, bytes[] calldata retdata) external;function mockCalls( address where, uint256 value, bytes calldata data, bytes[] calldata retdata) external;Description
Section titled “Description”Mocks all calls to an address where if the call data either strictly or loosely
matches data and returns different data for each call based on the retdata
array values.
See mockCall for more information on mocking calls and
matching precedence.
Examples
Section titled “Examples”Mocking multiple balanceOf(address) calls:
function testMockCall() public { bytes[] memory mocks = new bytes[](2); mocks[0] = abi.encode(2 ether); mocks[1] = abi.encode(1 ether);
vm.mockCalls( address(0), abi.encodeWithSelector(IERC20.balanceOf.selector, address(1)), mocks );
assertEq(IERC20(address(0)).balanceOf(address(1)), 2 ether); assertEq(IERC20(address(0)).balanceOf(address(1)), 1 ether);}Mocking multiple calls with msg.value:
function testMockCallsWithMsgValue() public { bytes[] memory mocks = new bytes[](2); mocks[0] = abi.encode(2 ether); mocks[1] = abi.encode(1 ether);
vm.mockCalls( address(0), 1 ether, abi.encodeWithSelector(DexPool.swapETHForToken.selector), mocks );
uint tokenAmount1 = DexPool(address(0)).swapETHForToken{ value: 1 ether }(); uint tokenAmount2 = DexPool(address(0)).swapETHForToken{ value: 1 ether }();
assertEq(tokenAmount1, 2 ether); assertEq(tokenAmount2, 1 ether);}