Last active
April 5, 2022 19:31
-
-
Save phuson/7fc3ac1f8abde377e8374609c4971967 to your computer and use it in GitHub Desktop.
Since OpenZeppelin AccessControl's dynamic error messaging changes based on the sender's address and the checked role, it's been a challenge to write tests against it. As part of my solution, I wrote a utility function to automate the error message generation for my tests in Foundry Forge.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getEncodedAccessControlError(address account, bytes32 role) | |
public | |
pure | |
returns (bytes memory) | |
{ | |
return | |
abi.encodePacked( | |
"AccessControl: account ", | |
Strings.toHexString(uint160(account), 20), | |
" is missing role ", | |
Strings.toHexString(uint256(role), 32) | |
); | |
} | |
// test in Forge | |
function testAccessControl() public { | |
vm.startPrank(user); | |
vm.expectRevert( | |
getEncodedAccessControlError(user, ADMIN_ROLE) | |
); | |
contract.doSomethingThatRequiresAdminRole(); | |
vm.stopPrank(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment