Foundry – vm.expectRevert Not Working as Expected

forgefoundrytestingunittesting

Here is the file I am working with SubscribeBySig.t.sol . You can replicate the scenario by cloning the above repo, switch to the 230-migrate-comm-unit-tests branch and run forge test.

The problem is when I use vm.expectRevert knowing that the function call is goin to revert, I get the following error, means that the function didn't revert.
enter image description here

But the moment I comment out or remove the vm.expectRevert line from the test, I again get an error, this time indicating that the function call reverted, which is obviously what I expected.

enter image description here

But why didn't the expectRevert catch this error in the first place? What am I doing wrong? I have also tried to use expectRevert with the error message, but nothing changed.

UPDATE: The tests pass when I use another method which is prefixing the function name with testFail. But I want to use expectRevert, still not sure what's the issue.

Best Answer

Got the issue.

Actually, I was passing a function call as an argument to another function call, which would be called obviously before the actual function call, because of which the expectRevert method expected the argument to fail.enter image description here

Here is the solution if you wanna check out - Commit

Related Topic