Add mockCall and expectCall cheatcodes#403
Add mockCall and expectCall cheatcodes#403gakonst merged 11 commits intofoundry-rs:masterfrom onbjerg:mock-and-expect-call
mockCall and expectCall cheatcodes#403Conversation
| } | ||
|
|
||
| // handle mocked calls | ||
| if let Some(ret) = self.state().mocked_calls.get(&(code_address, input.clone())) { |
There was a problem hiding this comment.
this looks pretty expensive, not sure how to get around it thou
There was a problem hiding this comment.
which part of it you think is expensive? the clone? have you tried without it?
gakonst
left a comment
There was a problem hiding this comment.
high level lgtm - curious what @brockelmore thinks
| record() | ||
| accesses(address)(bytes32[],bytes32[]) | ||
| expectEmit(bool,bool,bool,bool) | ||
| mockCall(address,bytes,bytes) |
There was a problem hiding this comment.
I was thinking we could also have mockCall(address,bytes) as an override, for checking functions w/o return data in a more user friendly way. Wonder if we could do it such that if the bytes argument is only 8 bytes, that it matches with anything
There was a problem hiding this comment.
good points. how does abigen work with overrides?
There was a problem hiding this comment.
actually, i'm not sure i understand the first one, can you clarify? i don't see the use case. is it just to avoid a revert?
There was a problem hiding this comment.
I was thinking we could also have mockCall(address,bytes) as an override, for checking functions w/o return data in a more user friendly way.
Kinda wish this was this was added. Would be useful for mocking functions that don't have return (external contract calls not in my implementation)
| } | ||
|
|
||
| // handle mocked calls | ||
| if let Some(ret) = self.state().mocked_calls.get(&(code_address, input.clone())) { |
There was a problem hiding this comment.
which part of it you think is expensive? the clone? have you tried without it?
|
@onbjerg can you add the |
|
Should be good now |
Adds three new cheatcodes:
mockCall,expectCallandclearMockedCalls. One is used to return dummy data from either an existing or non-existing contract, and the other is used to assert that a call was made to a specified address with certain calldata.For both
mockCallandexpectCall, if the first bytes parameter (which is the expected calldata) is only 4 bytes long then it is treated as a selector.mockCallthis means that any other data in the call is ignored and the result is mocked for an entire function.expectCallthis means that any call to the specified address with the specified selector results in the check passing.If the calldata is more than 4 bytes long, then the entirety of the calldata is matched for both.
Questions
Closes https://github.com/gakonst/foundry/issues/382