Unit test done

This commit is contained in:
2024-08-06 16:14:38 +08:00
parent 074260c945
commit aefe632675
6 changed files with 246 additions and 34 deletions
@@ -126,24 +126,89 @@ describe("FundMe", function () {
* Deployer = funder
* fundMe and replace with ethers, important to getBalance
*/
// Arrange
const startingFundMeBalance = await fundMe.provider.getBalance(fundMe.address);
const startingDeployerBalance = await fundMe.provider.getBalance(deployer);
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address,
);
const startingDeployerBalance =
await fundMe.provider.getBalance(deployer);
// Act
const transactionResponse = await fundMe.withdraw();
const transactionReceipt = await transactionResponse.wait(1);
const endingFundMeBalance = await fundMe.provider.getBalance(fundMe.address);
const endingDeployerBalance = await fundMe.provider.getBalance(deployer);
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address,
);
const endingDeployerBalance =
await fundMe.provider.getBalance(deployer);
const {gasUsed, effectiveGasPrice } = transactionReceipt; //gasUsed = "0x8b14"; effectiveGasPrice = "0x4e4814ec";
const { gasUsed, effectiveGasPrice } = transactionReceipt; //gasUsed = "0x8b14"; effectiveGasPrice = "0x4e4814ec";
const gasCost = gasUsed.mul(effectiveGasPrice);
// Assert
assert.equal(endingFundMeBalance, 0);
assert.equal(startingFundMeBalance.add(startingDeployerBalance), endingDeployerBalance.add(gasCost).toString())
assert.equal(
startingFundMeBalance.add(startingDeployerBalance),
endingDeployerBalance.add(gasCost).toString(),
);
});
it("Allow us to withdraw with multiple funders", async function () {
const accounts = await ethers.getSigners();
for (let a = 0; a < 6; a++) {
const fundMeConnectedContract = await fundMe.connect(
accounts[a],
);
await fundMeConnectedContract.fund({ value: eth1 });
}
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address,
);
const startingDeployerBalance =
await fundMe.provider.getBalance(deployer);
// Act
const transactionResponse = await fundMe.withdraw();
// Let's comapre gas costs :)
// const transactionResponse = await fundMe.withdraw()
const transactionReceipt = await transactionResponse.wait();
const { gasUsed, effectiveGasPrice } = transactionReceipt;
const withdrawGasCost = gasUsed.mul(effectiveGasPrice);
console.log(`GasCost: ${withdrawGasCost}`);
console.log(`GasUsed: ${gasUsed}`);
console.log(`GasPrice: ${effectiveGasPrice}`);
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address,
);
const endingDeployerBalance =
await fundMe.provider.getBalance(deployer);
// Assert
assert.equal(
startingFundMeBalance.add(startingDeployerBalance).toString(),
endingDeployerBalance.add(withdrawGasCost).toString(),
);
// Make a getter for storage variables
await expect(fundMe.funders(0)).to.be.reverted;
for (i = 1; i < 6; i++) {
assert.equal(
await fundMe.addressToAmountFunded(accounts[i].address),
0,
);
}
});
it("Only allow the owner to withdraw", async function () {
const accounts = await ethers.getSigners();
const attacker = accounts[1];
const attacherConnectedContract = await fundMe.connect(attacker);
await expect(attacherConnectedContract.withdraw()).to.be.reverted;
await expect(
attacherConnectedContract.withdraw(),
).to.be.revertedWith("FundMe__NotOwner");
});
});
});