Lesson: 9 || Unit testing for "returns false if enough time hasn't passed" fails [AssertionError: Unspecified AssertionError] #5915
-
My test is like this: it("Returns false if enough time hasn't passed", async function () {
await raffle.enterRaffle({ value: raffleEntranceFee });
await network.provider.send("evm_increaseTime", [interval.toNumber() - 1]);
await network.provider.request({ method: "evm_mine", params: []});
const { upkeepNeeded } = await raffle.callStatic.checkUpkeep("0x");
console.log(`UpKeepNeeded: ${upkeepNeeded}`);
assert(!upkeepNeeded);
}); The test doesn't work when I subtract 1 from the interval time. AssertionError: Unspecified AssertionError But when I subtract 2 from the interval time it works just fine. It works fine for the following: it("Returns false if enough time hasn't passed", async function () {
await raffle.enterRaffle({ value: raffleEntranceFee });
await network.provider.send("evm_increaseTime", [interval.toNumber() - 2]);
await network.provider.request({ method: "evm_mine", params: []});
const { upkeepNeeded } = await raffle.callStatic.checkUpkeep("0x");
console.log(`UpKeepNeeded: ${upkeepNeeded}`);
assert(!upkeepNeeded);
}); Can anyone explain why it is happening? My understanding is this the deployment and all other calculations take the missing 1 minute and as a result when it is checked it finds the interval to be over and thus returns true. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi, you can do
and
in your test scripts to find out what the actual timestamps are and what's going wrong. I was having the same issue, and I found that when my contract was deployed it saved Which means that the time had increased by 2 since the lottery initialised the |
Beta Was this translation helpful? Give feedback.
-
Thank you @Chevy3000 for the explanation. |
Beta Was this translation helpful? Give feedback.
Hi, you can do
const lotteryInitTimestamp = await lottery.getLatestTimeStamp()
console.log(lotteryInitTimestamp)
and
let blockTimestamp = (await ethers.provider.getBlock('latest')).timestamp
console.log(blockTimestamp)
in your test scripts to find out what the actual timestamps are and what's going wrong.
I was having the same issue, and I found that when my contract was deployed it saved
s_lastTimeStamp
as 1691781702 but when I calledconsole.log(blockTimestamp)
before running that test I got back 1691781703then after running the
enterLottery
function I ranconsole.log(blockTimestamp)
again and it had again increased by one to 1691781704.Which means that the time had increased by 2 sin…