Solidity – Why Foundry Breaks When Function Names Are Changed: Detailed Explanation

foundrysolidity

Hope you are all doing great
I have this weird problem with naming my test functions in foundry, I am watching Patrick Collins New Videos collections.
in lesson 7(), patrick wrote a test name testFundFailsWithoutEnoughETH (YouTube link to exact spot) to check if fund() function revert in case of not giving an enough value, and IT WORKS just like the videos.
I changed the name of the function to testFailWithoutEnoughETH() and now the function does NOT WORK.

Patrick's Code:

    function testFundFailWithoutEnoughETH() public {
        vm.expectRevert(); // this like tells solidity that we expect the next line to revert
        // fundMe.fund(); // send the function without enough eth, we must send at least MINIMUM_USD/ETH
    }

Terminal output for Patrik's code: (As expected, The test fails)

[⠑] Compiling...
[⠢] Compiling 1 files with 0.8.20
[⠆] Solc 0.8.20 finished in 1.30s
Compiler run successful!

Running 1 test for test/FundMeTest.t.sol:FundMeTest
[FAIL. Reason: Call did not revert as expected] testFundFailWithoutEnoughETH() (gas: 3045)
Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 2.98ms
Ran 1 test suites: 0 tests passed, 1 failed, 0 skipped (1 total tests)

Failing tests:
Encountered 1 failing test in test/FundMeTest.t.sol:FundMeTest
[FAIL. Reason: Call did not revert as expected] testFundFailWithoutEnoughETH() (gas: 3045)

Encountered a total of 1 failing tests, 0 tests succeeded

My Code:

    function testFailWithoutEnoughETH() public {
        vm.expectRevert(); // this like tells solidity that we expect the next line to revert
        // fundMe.fund(); // send the function without enough eth, we must send at least MINIMUM_USD/ETH
    }

Terminal output for My Code: (MY Code Does Not Fail)

[⠑] Compiling...
[⠒] Compiling 1 files with 0.8.20
[⠢] Solc 0.8.20 finished in 1.26s
Compiler run successful!

Running 1 test for test/FundMeTest.t.sol:FundMeTest
[PASS] testFailWithoutEnoughETH() (gas: 3112)
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 1.24ms
Ran 1 test suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)

Fund function:

function fund() public payable {
        require(msg.value.getConversionRate(s_priceFeed) >= MINIMUM_USD, "You need to spend more ETH!");
        // require(PriceConverter.getConversionRate(msg.value) >= MINIMUM_USD, "You need to spend more ETH!");
        addressToAmountFunded[msg.sender] += msg.value;
        funders.push(msg.sender);
}

My big concern is that i am suppose to test my contracts, and now i dont know even if my test are reliable or not, because they may act different with different names.

Can someone help me?

Best Answer

This is because of the fact that the testFail is a reserved keyword for foundry.

Take it this way. So, whenever we start the function name with test, foundry knows that this is a test function and it needs to run the function.

Similarly, when we start the name with testFail, foundry knows that this function is a test function that is supposed to revert.

So basically testFail is a keyword to tell foundry that we are expecting this function to revert.

When you are writing testFail, you don't need to write vm.expectRevert again

Watch this small video, and it will be clear. https://youtu.be/yY9lL4Jxkd8

Related Topic