forge-std
forge-std copied to clipboard
Add a way to enforce that a mocked function is called with certain arguments
Component
Forge
Describe the feature you would like
Currently tests don't break if a mocked function is called with different arguments to what was expected.
Currently for example:
cheats.mockCall(
address(mocker),
abi.encodeWithSelector(
SomeContract.someFunction.selector,
5
),
abi.encode(6)
);
With the above code it will return 6 if the function is called with an argument of 5 or anything else, additionally it won't fail if it isn't called with 5 (which is something you sometimes want to check.
I propose adding a function called something like mockCallStrict to the cheats so that tests can be more strict about this.
Additional context
This is a feature of Smock: https://smock.readthedocs.io/en/latest/fakes.html#asserting-call-arguments
You might be interested in expectCall instead. I don't think mockCall should be a superset of expectCall. Sometimes you definitely do want to only mock for a certain parameter, but still retain the ability to get real data for all other cases. If you want a stricter version perhaps this is a good addition to Forge Std instead as a wrapper around expectCall + mockCall
adding a mock+expect combination in forge-std makes sense to me
Thanks, that makes sense to me too.
I won't make any changes to the std lib right now, but I'm working on some code-gen stuff that might be useful for others once it is done to assist with mocking. Maybe that can be upstreamed at some point if it is useful.
Also FYI - my codegen work will allow mocking of internal functions without needing to move the the REVM as mentioned in this PR - https://github.com/foundry-rs/foundry/issues/432 .
Like I say, I'll definitely share my work on that in the coming weeks/months :+1:
@brockelmore Perhaps we should transfer this to Forge Std as well? Or maybe just close it and open up a new issue? Wdyt?