Lesson: 9 :- Error while running unit test #2014
-
ERROR
According to error I think the Lottery is still open while running this test don't no why? Test CODE - it("Can't Enter the Lottery when its in closed state", async () => {
await lottery.enterLottery({ value: lotteryEntranceFee })
await network.provider.send("evm_increaseTime", [interval.toNumber() + 1])
await network.provider.send("evm_mine", [])
await lottery.performUpkeep([])
await expect(
lottery.enterLottery({ value: lotteryEntranceFee })
).to.be.revertedWith("Lottery__NotOpen")
}) My Solidity Code for this Test - enterLottery function enterLottery() public payable {
if (msg.value < i_entranceFee) {
revert Lottery__SendMoreToEnterLottery();
}
// if lottery is not open for any reason
if (s_lotteryState != LotteryState.OPEN) {
revert Lottery__NotOpen();
}
s_players.push(payable(msg.sender));
// Emit an event when we update a dynamic array or mapping
emit LotteryEnter(msg.sender);
} checkUpkeepfunction checkUpkeep(
bytes memory /*checkData*/
)
public
view
override
returns (
bool upKeepNeeded,
bytes memory /*performData*/
)
{
bool isOpen = LotteryState.OPEN == s_lotteryState;
bool timePassed = ((block.timestamp - s_lastTimeStamp) > i_interval);
bool hasPlayers = s_players.length > 0;
bool hasBalance = address(this).balance > 0;
bool upKeepNeeded = (isOpen && timePassed && hasPlayers && hasBalance);
} performUpkeep function performUpkeep(
bytes calldata /*performData*/
) external override {
(bool upKeepNeeded, ) = checkUpkeep("");
if (!upKeepNeeded) {
revert Lottery__UpKeepNotNeeded(
address(this).balance,
s_players.length,
uint256(s_lotteryState)
);
}
s_lotteryState = LotteryState.CALCULATING;
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gaslane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
emit RequestedLotteryWinner(requestId);
}
Any help will be appreciated Thanks in Advance!! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 13 replies
-
From the revert error, I'm guessing the
as I have below and see if anything changes.
|
Beta Was this translation helpful? Give feedback.
-
Looks like it fails in the performUpkeep when it checks for (!upkeepNeeded). which leaves the timePassed. What is your interval set to? it is in the helper-hardhat-config.js and set via the 01-deploy-raffle.js |
Beta Was this translation helpful? Give feedback.
-
Try increasing time period for a block await network.provider.send("evm_increaseTime", [interval.toNumber() + 5]) |
Beta Was this translation helpful? Give feedback.
-
delete the bool in row 106 : bool upKeepNeeded = (timePassed && isOpen && hasPlayers && hasBalance);
add "await" in row 77: const lotteryState = await lottery.getLotteryState() |
Beta Was this translation helpful? Give feedback.
From the revert error, I'm guessing the
checkUpkeep()
function returns false. Could you try rewriting these lineas I have below and see if anything changes.