-
Notifications
You must be signed in to change notification settings - Fork 867
Document vm.mockCalls
#1325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document vm.mockCalls
#1325
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| ## `mockCalls` | ||
|
|
||
| ### Signature | ||
|
|
||
| ```solidity | ||
| function mockCalls(address where, bytes calldata data, bytes[] calldata retdata) external; | ||
| ``` | ||
|
|
||
| ```solidity | ||
| function mockCalls( | ||
| address where, | ||
| uint256 value, | ||
| bytes calldata data, | ||
| bytes[] calldata retdata | ||
| ) external; | ||
| ``` | ||
|
|
||
| ### 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`](./mock-call.md) for more information on mocking calls and | ||
| matching precedence. | ||
|
|
||
| > ℹ️ **Note** | ||
| > | ||
| > Any invocation of the mocked call beyond the number of elements in `retdata` | ||
| > will receive the last `retdata` element in response. `clearMockedCalls` can be | ||
| > called to clear the mock | ||
|
|
||
| ### Examples | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also have an example that uses the version with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add and update you
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an example with |
||
|
|
||
| Mocking multiple `balanceOf(address)` calls: | ||
|
|
||
| ```solidity | ||
| 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`: | ||
|
|
||
| ```solidity | ||
| 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); | ||
| } | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the version with
uint256 msgValueshould also be added, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not sure, since the
mockCallwithmsgValueis not included. By I can include it if you think it should be documented. In that case I should probably add it formockCallas well?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added both
mockCallandmockCallswith msg.value, lmk if is ok.