• YouTube Channel
  • System Status
  • VS Code Extension
  • Missing Revert Reason Tests in Test Functions

    Overview

    What are Missing Revert Reason Tests?

    Missing revert reason tests occur when test functions for smart contracts do not properly verify the specific reasons for function reverts. This can lead to incomplete testing of contract behavior and potentially miss critical validation issues.

    Why are Missing Revert Reason Tests Problematic?

    Missing revert reason tests in test functions can lead to several issues:

    Technical Example of Missing Revert Reason Tests

    
    pragma solidity ^0.8.0;
    import "./test.sol";
    
    contract TokenTest is Test {
        Token token;
    
        function setUp() public {
            token = new Token();
        }
    
        function testInvalidTransfer(address to, uint256 amount) public {
            vm.expectRevert();
            token.transfer(to, amount);
            // Missing check for specific revert reason
        }
    }
    
    

    In this example, the test function only checks that the transfer reverts, but doesn't verify the specific reason for the revert.

    Technical Example with Proper Revert Reason Tests

    
    pragma solidity ^0.8.0;
    import "./test.sol";
    
    contract TokenTest is Test {
        Token token;
    
        function setUp() public {
            token = new Token();
        }
    
        function testInvalidTransfer(address to, uint256 amount) public {
            vm.expectRevert("Insufficient balance");
            token.transfer(to, amount);
        }
    
        function testZeroAddressTransfer(uint256 amount) public {
            vm.expectRevert("Invalid recipient");
            token.transfer(address(0), amount);
        }
    }
    
    

    In this improved version, the test functions use `vm.expectRevert` with specific error messages to verify that the contract reverts for the correct reasons under different invalid conditions.