You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vm.expectRevert("StdChains getChain(string): Chain with alias \"does_not_exist\" not found.");
stdChainsMock.exposed_getChain("does_not_exist");
}
Why do we need to use mocks to properly test the revert?
Can't we just test revert like this? ->
functiontest_RevertIf_ChainNotFound()public{vm.expectRevert("StdChains getChain(string): Chain with alias \"does_not_exist\" not found.");getChain("does_not_exist");}
I have tried changing tests to 2nd approach and tests are passing. I am not sure if I am missing something.
I will open the PR with changes on the StdChains.t.sol so you can get a closer look on what do I mean. Please let me know your thoughts on this :)
The text was updated successfully, but these errors were encountered:
We intentionally use mocks because vm.expectRevert is intended to be used on the next call (or contract creation). If you remove the mocks and directly call the internal functions, you JUMP to that function. This was accidentally supported by foundry, but not a lot of people rely on it so it's not easy to remove. It's a big footgun because if you have two consecutive expectRevert calls with jumps, the test stops executing. So:
// This one is ok
vm.expectRevert("StdChains getChain(string): Chain with alias \"does_not_exist\" not found.");
getChain("does_not_exist");
// All code below here never executes! This is dangerous
vm.expectRevert("StdChains getChain(string): Chain with alias \"does_not_exist\" not found.");
getChain("does_not_exist");
I checked the forge book quickly and didn't see this documented, cc @klkvr@grandizzy@Evalir for more info
In test cases when revert is expected, tests are following this pattern:
MockContract
MockContract
that is supposed to revert.As far as I saw, mocks are used only for revert cases.
Quick example :
forge-std/test/StdChains.t.sol
Lines 86 to 94 in beb836e
Why do we need to use mocks to properly test the revert?
Can't we just test revert like this? ->
I have tried changing tests to 2nd approach and tests are passing. I am not sure if I am missing something.
I will open the PR with changes on the
StdChains.t.sol
so you can get a closer look on what do I mean. Please let me know your thoughts on this :)The text was updated successfully, but these errors were encountered: