Lesson 7: Error while testing: invalid address or ENS name #1953
-
Hello I have 5 errors while testing. It relates to the const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect } = require("chai")
const { developmentChains } = require("../../helper-hardhat-config")
!developmentChains.includes(network.name)
? describe.skip
: describe("FundMe", async function() {
let fundMe
let deployer
let mockV3Aggregator
let sendValue = ethers.utils.parseEther("1") // 1 ETH
beforeEach(async function() {
//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 corectly", async function() {
const response = await fundMe.getPriceFeed()
assert.equal(response, mockV3Aggregator.address)
})
})
describe("fund", async function() {
it("Fails if you don't 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.getAddressToAmountFunded(
deployer
)
assert.equal(response.toString(), sendValue.toString())
})
it("Adds funder to array of getFunder", async function() {
await fundMe.fund({ value: sendValue })
const funder = await fundMe.getFunder[0]
assert.equal(funder, deployer)
})
})
describe("withdraw", async function() {
beforeEach(async function() {
await fundMe.fund({ value: sendValue })
})
it("Withdraw ETH from a single funder", async function() {
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
const startingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
const transactionResponse = await fundMe.withdraw()
const transactionReciept = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReciept
const gasCost = gasUsed.mul(effectiveGasPrice)
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
const endingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
assert.equal(endingFundMeBalance, 0)
assert.equal(
startingFundMeBalance.add(startingDeployerBalance),
endingDeployerBalance.add(gasCost).toString()
)
})
it("allows us to withdraw with multiple getFunder", async function() {
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
)
const transactionResponse = await fundMe.withdraw()
const transactionReciept = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReciept
const gasCost = gasUsed.mul(effectiveGasPrice)
assert.equal(endingFundMeBalance, 0)
assert.equal(
startingFundMeBalance.add(startingDeployerBalance),
endingDeployerBalance.add(gasCost).toString()
)
await expect(fundMe.getFunder(0)).to.be.reverted
for (i = 1; i < 6; i++) {
assert.equal(
await fundMe.getAddressToAmountFunded(
accounts[i].address
),
0
)
}
})
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")
})
it("cheaperWithdraw testing", async function() {
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
)
const transactionResponse = await fundMe.cheaperWithdraw()
const transactionReciept = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReciept
const gasCost = gasUsed.mul(effectiveGasPrice)
assert.equal(endingFundMeBalance, 0)
assert.equal(
startingFundMeBalance.add(startingDeployerBalance),
endingDeployerBalance.add(gasCost).toString()
)
await expect(fundMe.getFunder(0)).to.be.reverted
for (i = 1; i < 6; i++) {
assert.equal(
await fundMe.getAddressToAmountFunded(
accounts[i].address
),
0
)
}
})
it("cheaperWithdraw ETH from a single funder", async function() {
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
const startingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
const transactionResponse = await fundMe.cheaperWithdraw()
const transactionReciept = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReciept
const gasCost = gasUsed.mul(effectiveGasPrice)
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
)
const endingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
assert.equal(endingFundMeBalance, 0)
assert.equal(
startingFundMeBalance.add(startingDeployerBalance),
endingDeployerBalance.add(gasCost).toString()
)
})
})
}) And here are my errors that I get after running
Thank you for taking your time to read thru this and helping me. I'm really greateful because this community saved me a couple of times now. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
@Niksome send me your repo link, I will look into that. |
Beta Was this translation helpful? Give feedback.
-
Rewrite the line |
Beta Was this translation helpful? Give feedback.
Rewrite the line
deployer = await getNamedAccounts().deployer
intodeployer = (await getNamedAccounts()).deployer