Lesson 7: Error in the second test for "withdraw" function #1579
-
I can't figure out why my it("allows us to withdraw with multiple funders", async function () {
//Arrange
const accounts = await ethers.getSigners()
for (let i = 0; i < 6; i++) {
const fundMeConnectedContract = await fundMe.connect(
accounts[i]
)
fundMeConnectedContract.fund({ value: sendValue })
}
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
console.log(
"startingFundMeBalance: " +
startingFundMeBalance / 1000000000000000000
)
const startingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
console.log(
"startingDeployerBalance: " +
startingDeployerBalance / 1000000000000000000
)
//Act
const transactionResponse = await fundMe.withdraw()
const transactionReceipt = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReceipt
const gasCost = gasUsed.mul(effectiveGasPrice)
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
const endingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
console.log(
"Final Deployer Balance: " +
startingDeployerBalance
.add(startingFundMeBalance)
.toString() /
1000000000000000000
)
console.log(
"endingDeployerBalance: " +
endingDeployerBalance.add(gasCost).toString() /
1000000000000000000
)
//Assert
assert.equal(endingFundMeBalance, 0)
assert.equal(
startingDeployerBalance.add(startingFundMeBalance).toString(),
endingDeployerBalance.add(gasCost).toString()
) /**@notice add function makes it easier for adding big numbers */
//Check if funders array is reset properly
await expect(fundMe.funders(0)).to.be.reverted
for (let i = 1; i < 6; i++) {
assert.equal(
await fundMe.addressToAmountFunded(accounts[i].address),
0
)
}
}) and this is the output:
when I run this command:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I forgot to add the This is before correction: for (let i = 0; i < 6; i++) {
const fundMeConnectedContract = await fundMe.connect(
accounts[i]
)
fundMeConnectedContract.fund({ value: sendValue })
} and this is after correction: for (let i = 1; i < 6; i++) {
const fundMeConnectedContract = await fundMe.connect(
accounts[i]
)
await fundMeConnectedContract.fund({ value: sendValue })
} Could someone explain in detail why the code didn't work when the |
Beta Was this translation helpful? Give feedback.
-
Without |
Beta Was this translation helpful? Give feedback.
I forgot to add the
await
keyword to thefundMeConnectedContract.fund({ value: sendValue })
line at the starting of the test "allows us to withdraw with multiple funders". I also changed the for loop constraints fromfor (let i = 0; i < 6; i++)
tofor (let i = 1; i < 6; i++)
.This is before correction:
and this is after correction: