assertion error #901
-
hi i am new to coding. i have the exact same code as written in the course in FundMe.test.js and when i run ( yarn hardhat test --grep "Only allows the owner to withdraw" ) this is the error i get : AssertionError: Expected transaction to be reverted with reason 'FundMe__NotOwner', but it reverted with a custom error PS: i did change the: notOwner , to : FundMe__NotOwner, inside FundMe.sol this is my code: although i know my error is in the last 5-6 lines i will post all my test file. please help me find the error! const { assert, expect } = require("chai")
const { deployments, ethers, getNamedAccounts } = require("hardhat")
// u can use console.log in solidity import hardhat/consolelog shihek
describe("FundMe", async function () {
let fundMe
let deployer
let mockV3Aggregator
const sendValue = ethers.utils.parseEther("1") // 1eth
beforeEach(async function () {
//deploy our fund me contract using hardhat deploy
// const accounts = await ethers.getSigners()
// const accountZero = accounts[0]
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 addresses 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("updates 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("Withdraw ETH from a single founder", async function () {
//arrange arrange yaane setup
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
const startingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
//act act yaane run kaan
const transactionResponse = await fundMe.withdraw() //calling withdraw function
const transactionReceipt = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReceipt
const gasCost = gasUsed.mul(effectiveGasPrice) // . mul yaane time
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(), // . add hiye badel l + eza ra2em ktir kbir w lstring cz big nbs r objects
endingDeployerBalance.add(gasCost).toString() // u can get gas cost men trans receipt w bdk taamldebug ya hmar
)
})
it("allows us to withdraw with multiple funders", async function () {
//arrange
const accounts = await ethers.getSigners()
for (let i = 1; i < 6; i++) {
const fundMeConnectedContract = await fundMe.connect(
accounts[i]
)
await fundMeConnectedContract.fund({ value: sendValue })
}
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
const startingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
//act
const transactionResponse = await fundMe.withdraw() //calling withdraw function
const transactionReceipt = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReceipt
const gasCost = gasUsed.mul(effectiveGasPrice) // . mul yaane times hawde nafs lshi mtl abel l act
//aasert
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(), // hawde mtl l abel ya hmar
endingDeployerBalance.add(gasCost).toString() // u can get gas cost men trans receipt w bdk taamldebug ya hmar
)
// make sure funders r 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 = 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.
Replies: 4 comments 11 replies
-
You are trying to withdraw a fund that doesn't exist, so Its probably throwing an error of not enough funds. Treat each test case like a newly deployed contract. This is what i mean. Add this line to the test case "allows only owner to withdraw" |
Beta Was this translation helpful? Give feedback.
-
@chrismogab Verify from contract that you are throwing the same error you mentioned in the test. |
Beta Was this translation helpful? Give feedback.
-
Ran into this problem as well and it doesn't seem like using Trying to format the reason string to what was being logged didn't seem to work either so I dug into the Assertion interface and notice it has another method I reformatted my test to use this other method and fixed this issue for me. Here's the final version of that unit test: it("Only allows the owner to withdraw", async function() {
const accounts = await ethers.getSigners()
const attacker = accounts[1]
const attackerConnectedContract = await fundMe.connect(attacker)
await expect(
attackerConnectedContract.withdraw()
).to.be.revertedWithCustomError(fundMe, "FundMe__NotOwner")
}) Link to plugin for reference: https://hardhat.org/hardhat-chai-matchers/docs/overview |
Beta Was this translation helpful? Give feedback.
-
I use Hardhat 2.10.1 and came across the same error. Here's the solution:
|
Beta Was this translation helpful? Give feedback.
I use Hardhat 2.10.1 and came across the same error. Here's the solution: