Skip to content

Commit

Permalink
write test for send failed
Browse files Browse the repository at this point in the history
  • Loading branch information
allwin199 committed Jan 30, 2024
1 parent 238b781 commit 998e762
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/mocks/MocksSendFailed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

contract MocksSendFailed {
fallback() external {
revert();
}
}
26 changes: 26 additions & 0 deletions test/unit/RaffleTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {HelperConfig} from "../../script/HelperConfig.s.sol";
import {Raffle} from "../../src/Raffle.sol";
import {VRFCoordinatorV2Interface} from "@chainlink/contracts/src/v0.8/vrf/interfaces/VRFCoordinatorV2Interface.sol";
import {VRFCoordinatorV2Mock} from "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2Mock.sol";
import {MocksSendFailed} from "../mocks/MocksSendFailed.sol";

contract RaffleTest is Test {
//////////////////////////////////////////////////////////
Expand Down Expand Up @@ -319,4 +320,29 @@ contract RaffleTest is Test {
uint256 winnerBalanceAfterWinning = winnerBalanceAfterEntering + prize;
assertEq(winnerBalance, winnerBalanceAfterWinning);
}

function test_SendingMoneyTo_WinnerFailed() public {
MocksSendFailed mocksSendFailed = new MocksSendFailed();

vm.deal(address(mocksSendFailed), STARTING_PLAYER_BALANCE);

vm.startPrank(address(mocksSendFailed));
raffle.enterRaffle{value: entranceFee}();
vm.stopPrank();

vm.warp(block.timestamp + interval + 10);
vm.roll(block.number + 1);

// let's get requestId using recordLogs
vm.recordLogs();
raffle.performUpkeep("");

Vm.Log[] memory entries = vm.getRecordedLogs();
bytes32 requestId = entries[1].topics[1];

// vm.expectRevert(Raffle.Raffle__Sending_RaffleAmountTo_WinnerFailed.selector);
// pretend to be the chainlink vrf to get random number & pick winner
// fulfillRandomWords(uint256 _requestId, address _consumer) inside vrfMock
VRFCoordinatorV2Mock(vrfCoordinatorAddress).fulfillRandomWords(uint256(requestId), address(raffle));
}
}

0 comments on commit 998e762

Please sign in to comment.