LESSON7: Getting Error while testing onlyOwner Modifier #1371
-
i am getting this error while testing last test for onlyOwner Can Withdraw modifier wile everything looks ok in my code const { deployments, ethers, getNamedAccounts } = require("hardhat");
const { assert, expect } = require("chai");
describe("FundMe", async function () {
let fundMe;
let deployer;
let mockV3Aggregator;
const sendValue = ethers.utils.parseEther("1");
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]);
fundMe = await ethers.getContract("FundMe", deployer);
mockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer);
});
describe("constructor", async function () {
it("sets the aggregator address correctly", async function () {
const response = await fundMe.priceFeed();
assert.equal(response, mockV3Aggregator.address);
});
});
describe("fund", async function () {
it("fails if you dont send enough ETH", async function () {
await expect(fundMe.fund()).to.be.revertedWith(
"You Need to spend more eth"
);
});
it("updated the amount funded data structure", async function () {
await fundMe.fund({ value: sendValue });
const response = await fundMe.addressToAmountFunded(deployer);
assert.equal(response.toString(), sendValue.toString());
});
it("Adds funder to array of funders", async function () {
await fundMe.fund({ value: sendValue });
const funder = await fundMe.funders(0);
assert.equal(funder, deployer);
});
});
describe("withdraw", async function () {
beforeEach(async function () {
await fundMe.fund({ value: sendValue });
});
it("withdrew ETH from a single founder", async function () {
//Arrnage
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 { gasUsed, effectiveGasPrice } = transactionReceipt;
const gasCost = gasUsed.mul(effectiveGasPrice);
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
);
const endingDeployerBalance = await fundMe.provider.getBalance(deployer);
//assert
assert.equal(endingFundMeBalance, 0);
assert.equal(
startingFundMeBalance.add(startingDeployerBalance).toString(),
endingDeployerBalance.add(gasCost).toString()
);
});
it("allows us to withdraw with multiple funders", async function () {
//Arrange
const accounts = await ethers.getSigners();
for (let i = 1; i < 6; i++) {
const fundConnectContract = await fundMe.connect(accounts[i]);
await fundConnectContract.fund({ value: sendValue });
}
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 { gasUsed, effectiveGasPrice } = transactionReceipt;
const gasCost = gasUsed.mul(effectiveGasPrice);
//Asssert
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
);
const endingDeployerBalance = await fundMe.provider.getBalance(deployer);
//assert
assert.equal(endingFundMeBalance, 0);
assert.equal(
startingFundMeBalance.add(startingDeployerBalance).toString(),
endingDeployerBalance.add(gasCost).toString()
);
//Make Sure that the funders are reset properly
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 allows the Owner to Withdraw", async function () {
const accounts = ethers.getSigners();
const attacker = accounts[1];
const attackerConnectedContract = await fundMe.connect(attacker);
await expect(attackerConnectedContract.withdraw()).to.be.reverted;
});
});
}); |
Beta Was this translation helpful? Give feedback.
Answered by
alymurtazamemon
Jul 28, 2022
Replies: 2 comments 18 replies
-
Can be fixed by the following code: Note: You should have a custom error called FundMe__NotOwner in the main contract, make one if it is not there it("Only allows the owner to withdraw", async function () {
const accounts = await ethers.getSigners()
const fundMeConnectedContract = await fundMe.connect(
accounts[1]
)
await expect(
fundMeConnectedContract.withdraw()
).to.be.revertedWith("FundMe__NotOwner")
}) |
Beta Was this translation helpful? Give feedback.
7 replies
-
@ritesh798 push to GitHub and leave link here, I will check it. |
Beta Was this translation helpful? Give feedback.
11 replies
Answer selected by
ritesh798
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ritesh798 push to GitHub and leave link here, I will check it.