Cyfrin / foundry-smart-contract-lottery-cu

48 stars 42 forks source link

Issue with testPerformUpkeepRevertsIfCheckUpkeepIsFalse() #29

Closed dannweeeee closed 1 year ago

dannweeeee commented 1 year ago
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.

PatrickAlphaC commented 1 year ago

Can you:

  1. Make this a discusson on the full repo? https://github.com/smartcontractkit/full-blockchain-solidity-course-js/
  2. Follow this section for formatting questions? https://www.youtube.com/watch?t=19846&v=gyMwXuJrbJQ&feature=youtu.be
usgeeus commented 9 months ago

@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("");
    }