Hardhat – Fixing VM Exception Reverted with Reason String ‘Didn’t send enough!’ in Testing Contract

chaihardhatreverttestingunittesting

Getting this error while testing one of functions functionality in the contract

AssertionError: Expected transaction to be reverted with You need to spend more ETH!!, but other exception was thrown: Error: VM Exception while processing transaction: reverted with reason string 'Didn't send enough!'

I have used waffles chai-matchers.
This is my test.js

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

describe("FundMe", function () {
    let fundMe
    let deployer
    let mockV3Aggregator
    beforeEach(async function () {
        deployer = (await getNamedAccounts()).deployer
        await deployments.fixture(["all"]) 
        fundMe = await ethers.getContract("FundMe", deployer) 
    describe("fund", 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!!"
            )
        })
    })
})

this is my FundMe.sol contract's fund function

function fund() public payable {
        require(
            msg.value.getConversionRate(priceFeed) >= MINIMUM_USD,
            "Didn't send enough!"
        );
    }

Best Answer

I got this error because in my fundMe smart contract i used revert error in require is different than script, they both should be same. In require it is "Didn't send enough!" while in revert it is "You need to spend more ETH!!" So i have to make them same then it will work.

Related Topic