Skip to content

Commit 42da808

Browse files
authored
Document vm.mockCalls (#1325)
* Document vm.mockCalls * Add mockCall and mockCalls with `msg.value` param to cheatcodes readme * Add example in mockCalls for mocking with msg.value
1 parent 25c558f commit 42da808

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@
422422
- [`setNonce`](./cheatcodes/set-nonce.md)
423423
- [`getNonce`](./cheatcodes/get-nonce.md)
424424
- [`mockCall`](./cheatcodes/mock-call.md)
425+
- [`mockCalls`](./cheatcodes/mock-calls.md)
425426
- [`mockCallRevert`](./cheatcodes/mock-call-revert.md)
426427
- [`mockFunction`](./cheatcodes/mock-function.md)
427428
- [`clearMockedCalls`](./cheatcodes/clear-mocked-calls.md)

src/cheatcodes/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ interface CheatCodes {
307307
// function will be mocked.
308308
function mockCall(address, bytes calldata, bytes calldata) external;
309309
310+
/// Mocks a call to an address with a specific `msg.value`, returning specified data.
311+
/// Calldata match takes precedence over `msg.value` in case of ambiguity.
312+
function mockCall(address, uint256, bytes calldata, bytes calldata) external;
313+
314+
// Mocks multiple call to an address, returning specified data for each call.
315+
//
316+
// Calldata can either be strict or a partial match, e.g. if you only
317+
// pass a Solidity selector to the expected calldata, then the entire Solidity
318+
// function will be mocked.
319+
function mockCalls(address, bytes calldata, bytes[] calldata) external;
320+
321+
/// Mocks multiple calls to an address with a specific `msg.value`, returning specified data for each call.
322+
function mockCalls(address, uint256, bytes calldata, bytes[] calldata) external;
323+
310324
// Reverts a call to an address, returning the specified error
311325
//
312326
// Calldata can either be strict or a partial match, e.g. if you only

src/cheatcodes/environment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [`setNonce`](./set-nonce.md)
2222
- [`getNonce`](./get-nonce.md)
2323
- [`mockCall`](./mock-call.md)
24+
- [`mockCalls`](./mock-calls.md)
2425
- [`mockCallRevert`](./mock-call-revert.md)
2526
- [`mockFunction`](./mock-function.md)
2627
- [`clearMockedCalls`](./clear-mocked-calls.md)

src/cheatcodes/mock-calls.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)