forge-std icon indicating copy to clipboard operation
forge-std copied to clipboard

Add a way to enforce that a mocked function is called with certain arguments

Open JasoonS opened this issue 3 years ago • 5 comments

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

JasoonS avatar May 04 '22 10:05 JasoonS

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

onbjerg avatar May 04 '22 15:05 onbjerg

adding a mock+expect combination in forge-std makes sense to me

gakonst avatar May 04 '22 18:05 gakonst

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.

JasoonS avatar May 05 '22 11:05 JasoonS

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:

JasoonS avatar May 05 '22 11:05 JasoonS

@brockelmore Perhaps we should transfer this to Forge Std as well? Or maybe just close it and open up a new issue? Wdyt?

onbjerg avatar May 05 '22 15:05 onbjerg