PatrickAlphaC / hardhat-fund-me-fcc

82 stars 183 forks source link

Multiple Errors in "FundMe.test.js" #136

Closed SNEHAASHISH closed 1 year ago

SNEHAASHISH commented 1 year ago

Terminal Snaps

image image

FundMe.test.js:-

` const { assert, expect } = require("chai") const { deployments, ethers, getNamedAccounts } = require("hardhat")

describe("FundMe", async function () { let fundMe let deployer let mockV3Aggregator const sendValue = ethers.utils.parseEther("100") //"1000000000000000000" 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 addresses correctly", async function () {
        const response = await fundMe.priceFeed()
        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 amountFunded 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 () => {
        await fundMe.fund({ value: sendValue })
        const response = await fundMe.getFunder(0)
        assert.equal(response, 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 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.equal(endingFundMeBalance, 0)
        assert.equal(
            startingFundMeBalance.add(startingDeployerBalance).toString(),
            endingDeployerBalance.add(gasCost).toString()
        )
    })
    it("allows us to withdraw with multiple funders", 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 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.equal(endingFundMeBalance, 0)
        assert.equal(
            startingFundMeBalance.add(startingDeployerBalance).toString(),
            endingDeployerBalance.add(gasCost).toString()
        )

        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.revertedWith("FundMe__NotOwner")
    })
})

})

`

PatrickAlphaC commented 1 year ago

Can you:

  1. Make this a discusson on the full repo? https://github.com/smartcontractkit/full-blockchain-solidity-course-js/
  2. Follow this section for formatting questions? https://www.youtube.com/watch?t=19846&v=gyMwXuJrbJQ&feature=youtu.be