Lesson 7: Unit testing is failing #5988
Answered
by
alfaqi
Astronaut828
asked this question in
Q&A
-
Hello, for some reason all my testing is failing, I have made some changes upon research and according to the course repo but
my current file looks like this: // Unit tests are done locally
// testing small pieces of the code for correct behavior
const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect } = require("chai")
describe("FundMe", function () {
let fundMe
let deployer
let mockV3Aggregator
const sendValue = ethers.parseEther("1") // 1 ETH
beforeEach(async function () {
// deploy our contract
// using hardhat deploy
// const accounts = await ethers.getSigners() // another way to get deployer accounts
// deployer = accounts[0]
deployer = (await getNamedAccounts()).deployer
await deployments.fixture(["all"]) // will deploy everything in deploy folder
fundMe = await ethers.getContractAt("FundMe", deployer) // will get latest deployed contract
mockV3Aggregator = await ethers.getContractAt(
"MockV3Aggregator",
deployer
)
})
describe("constructor", function () {
it("Sets the aggregator address correctly", async function () {
const response = await fundMe.priceFeed()
assert.equal(response, mockV3Aggregator.target);
})
})
describe("fund", function () {
it("Fails if you dont send enough ETH", async function () {
/* expecting to fail */
await expect(fundMe.fund()).to.be.revertedWith(
"You need to spend more ETH!"
)
})
it("updated the amount 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
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.getAddress()
)
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.getAddress()
)
const endingDeployerBalance = await fundMe.provider.getBalance(
deployer
)
// Assert
assert.equal(endingFundMeBalance, 0)
assert.equal(
startingFundMeBalance.add(startingDeployerBalance).toString(),
endingDeployerBalance
)
})
})
}) my updated repo can be found at: Thank you guys for the help in advance. |
Beta Was this translation helpful? Give feedback.
Answered by
alfaqi
Aug 10, 2023
Replies: 1 comment 1 reply
-
require("@nomiclabs/hardhat-ethers")
fundMe = await ethers.getContract("FundMe", deployer)
mockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer)
it("Adds funder to array of funders"), after modification it("Adds funder to array of funders", async function () {
await fundMe.fund({ value: sendValue })
const funder = await fundMe.funders(0)
assert.equal(funder, deployer)
})
const startingFundMeBalance = await ethers.provider.getBalance(
fundMe.getAddress()
)
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Astronaut828
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hardhat.config.js
filegetContractAt()
usegetContract()
,)
after modification
fundMe.provider
useethers.provider
like this