0

In my smart contract there is a function:

function fund() public payable {
    require(
        msg.value.getConversionRate(priceFeed) >= MINIMUM_USD,
        "You need to spend more ETH!"
    );
    addressToAmountFunded[msg.sender] += msg.value;
    funders.push(msg.sender);
}

when I test fund() with is code:

beforeEach(async function () {
    deployer = (await getNamedAccounts()).deployer
    await deployments.fixture(["all"])
    fundMe = await ethers.getContractAt("FundMe", deployer)
    mockV3Aggregator = await ethers.getContractAt(
        "MockV3Aggregator",
        deployer
    )
})

describe("fund", async function () {
    it("Fails if you don't send enough ETH", async () => {
        await expect(fundMe.fund()).to.be.revertedWith(
            "You need to spend more ETH!"
        )
    })
})

I run yarn hardhat test then I get :

     Fails if you don't send enough ETH:
 AssertionError: Expected transaction to be reverted with reason 'You need to spend more ETH!', but it didn't revert
  at processTicksAndRejections (node:internal/process/task_queues:96:5)
  at Context.<anonymous> (test/unit/FundMe.test.js:27:13)

this is my code in github

2
  • I tried creating a file.sol to test require() for revert. But it still shows the same error Jan 3 at 0:54
  • I tested the fund() function on remix and it still revert Jan 3 at 1:07

2 Answers 2

0

To fix this, you need to specify the amount of ETH you want to send in your test, using the {value: amount} option in the fund () function call. For example, you can write something like this:

it("Fails if you don't send enough ETH", async () => {
    await expect(fundMe.fund({value: 10})).to.be.revertedWith(
        "You need to spend more ETH!"
    )
})

This will send 10 wei (the smallest unit of ETH) to the fund () function, which is likely to be less than the minimum USD amount required. You can adjust the value as needed to test different scenarios.

2
  • Tried adding { value: 10 } to fund() but the same error still occurs. Jan 3 at 0:52
  • Try with just reverted instead of revertedWith
    – Mehran Ra
    Jan 4 at 6:38
0

I changed the input of getContractAt() to abi and address of contract. with code:

beforeEach(async function () {
    deployer = (await getNamedAccounts()).deployer
    await deployments.fixture(["all"])

    const myFundMe = await deployments.get("FundMe")
    const mymockV3Aggregator = await deployments.get("MockV3Aggregator")

    fundMe = await ethers.getContractAt(myFundMe.abi, myFundMe.address)
    mockV3Aggregator = await ethers.getContractAt(
        mymockV3Aggregator.abi,
        mymockV3Aggregator.address
    )
})

Then I passed this test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.