|
| 1 | +## `mockCalls` |
| 2 | + |
| 3 | +### Signature |
| 4 | + |
| 5 | +```solidity |
| 6 | +function mockCalls(address where, bytes calldata data, bytes[] calldata retdata) external; |
| 7 | +``` |
| 8 | + |
| 9 | +```solidity |
| 10 | +function mockCalls( |
| 11 | + address where, |
| 12 | + uint256 value, |
| 13 | + bytes calldata data, |
| 14 | + bytes[] calldata retdata |
| 15 | +) external; |
| 16 | +``` |
| 17 | + |
| 18 | +### Description |
| 19 | + |
| 20 | +Mocks all calls to an address `where` if the call data either strictly or loosely |
| 21 | +matches `data` and returns different data for each call based on the `retdata` |
| 22 | +array values. |
| 23 | + |
| 24 | +See [`mockCall`](./mock-call.md) for more information on mocking calls and |
| 25 | +matching precedence. |
| 26 | + |
| 27 | +> ℹ️ **Note** |
| 28 | +> |
| 29 | +> Any invocation of the mocked call beyond the number of elements in `retdata` |
| 30 | +> will receive the last `retdata` element in response. `clearMockedCalls` can be |
| 31 | +> called to clear the mock |
| 32 | +
|
| 33 | +### Examples |
| 34 | + |
| 35 | +Mocking multiple `balanceOf(address)` calls: |
| 36 | + |
| 37 | +```solidity |
| 38 | +function testMockCall() public { |
| 39 | + bytes[] memory mocks = new bytes[](2); |
| 40 | + mocks[0] = abi.encode(2 ether); |
| 41 | + mocks[1] = abi.encode(1 ether); |
| 42 | +
|
| 43 | + vm.mockCalls( |
| 44 | + address(0), |
| 45 | + abi.encodeWithSelector(IERC20.balanceOf.selector, address(1)), |
| 46 | + mocks |
| 47 | + ); |
| 48 | +
|
| 49 | + assertEq(IERC20(address(0)).balanceOf(address(1)), 2 ether); |
| 50 | + assertEq(IERC20(address(0)).balanceOf(address(1)), 1 ether); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Mocking multiple calls with `msg.value`: |
| 55 | + |
| 56 | +```solidity |
| 57 | +function testMockCallsWithMsgValue() public { |
| 58 | + bytes[] memory mocks = new bytes[](2); |
| 59 | + mocks[0] = abi.encode(2 ether); |
| 60 | + mocks[1] = abi.encode(1 ether); |
| 61 | +
|
| 62 | + vm.mockCalls( |
| 63 | + address(0), |
| 64 | + 1 ether, |
| 65 | + abi.encodeWithSelector(DexPool.swapETHForToken.selector), |
| 66 | + mocks |
| 67 | + ); |
| 68 | +
|
| 69 | + uint tokenAmount1 = DexPool(address(0)).swapETHForToken{value: 1 ether}(); |
| 70 | + uint tokenAmount2 = DexPool(address(0)).swapETHForToken{value: 1 ether}(); |
| 71 | +
|
| 72 | + assertEq(tokenAmount1, 2 ether); |
| 73 | + assertEq(tokenAmount2, 1 ether); |
| 74 | +} |
| 75 | +``` |
0 commit comments