0

In my contract A I have a function foo which has a require statement

contract A {
  bool public requirement;
  
  function foo() public {
     require(requirement, "State not set correctly for continuation");
     ...
  }
}

Now I want to test this with Foundry

contract ATest is Test {
   function testFoo() public {
        // executing function with some account
        vm.prank(someAddress);
        A.foo(); // <- this will fail
    }
}

How should I do that?

1 Answer 1

0

This is pretty easy once found the documentation for that. It works for both, require and revert.

contract ATest is Test {
       function testFoo() public {
            // executing function with some account
            vm.prank(someAddress);
            vm.expectRevert(bytes("State not set correctly for continuation")); // <- add this
            A.foo(); // <- this will fail
        }
    }

For documentation go here: https://book.getfoundry.sh/cheatcodes/expect-revert

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.