-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat(cheatcodes): Add vm.mockCalls
to mock different return data for multiple calls
#9024
Conversation
Opened draft PRs for Foundry Book and Forge-Std. |
Thanks for your PR! Implementation looks good, small note in foundry-rs/forge-std#615 |
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.
lgtm, just a small comment. pending other reviews
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.
Thanks! LGTM - pending other reviewers
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.
Thank you, lgtm
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.
Thanks!
Closes #7467
Motivation
Today, we can mock the return value of a function call by using
mockCall
. Sometimes, it is useful to mock the same call twice with different return values.Consider the following function:
Both invocations of
token.balanceOf()
have the same calldata. This means we cannot use two differentmockCall
to test this behavior.With
mockCalls
we would test it like so:Solution
I changed
Cheatcodes::mocked_calls
to have a type ofHashMap<Address, BTreeMap<MockCallDataContext, VecDeque<MockCallReturnData>>>
, note theVecDeque<...>
.I then went in
inspector.rs
and modified the part which handles the mocked calls, to pop the front mock return data as long as the stack has more than one element. This kept backward-compatibility in such a way which minimized the amount of necessary changes.Cheatcodes checklist
The checklist from the cheatcodes development docs:
If a struct, enum, error, or event was added to Vm, update spec::Cheatcodes::new