Cyfrin / foundry-smart-contract-lottery-cu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with testPerformUpkeepRevertsIfCheckUpkeepIsFalse()

dannweeeee opened this issue · comments

Screenshot 2023-09-14 at 1 01 56 AM

when I forge test, all tests seems to work, but when I forge test --fork-url $SEPOLIA_RPC_URL, it gave this error. I understand that the error is because the currentBalance is 4e16 but it should be zero.

@dannweeeee @PatrickAlphaC
The raffle contract gets deployed to address '0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496'.
and there is 0.04 ETH (40000000000000000 wei) on that address. See this etherscan
That's why that test function fails.
So you'll need to add below to the constructor function.

uint256 balance = address(this).balance;
if (balance > 0) {
    payable(msg.sender).transfer(balance);
}

You can fix the test function without fixing the constructor function, as shown below, but it's not recommended.

    function testPerformUpkeepRevertsIfCheckUpkeepIsFalse() public {
        // Arrange
        uint256 currentBalance = address(raffle).balance;
        uint256 numPlayers = 0;
        uint256 raffleState = 0;
        // Act / Assert
        vm.expectRevert(
            abi.encodeWithSelector(
                Raffle.Raffle__UpkeepNotNeeded.selector,
                currentBalance,
                numPlayers,
                raffleState
            )
        );
        raffle.performUpkeep("");
    }