smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.4k stars 2.98k forks source link

Lesson:7: Error wile testing fund() function at 11:21 #5897

Open LeoFranklin015 opened 1 year ago

LeoFranklin015 commented 1 year ago

Discussed in https://github.com/smartcontractkit/full-blockchain-solidity-course-js/discussions/1257

Originally posted by **ritesh798** July 23, 2022 getting this error when i used expect from chai ```javascript const { assert, expect } = require("chai"); const { deployments, ethers, getNamedAccounts } = require("hardhat"); describe("FundMe", async () => { let fundMe; let deployer; let mockV3Aggregator; beforeEach(async () => { deployer = (await getNamedAccounts()).deployer; await deployments.fixture(["all"]); fundMe = ethers.getContract("FundMe", deployer); mockV3Aggregator = ethers.getContract("MockV3Aggregator", deployer); }); describe("Constructor", async () => { it("Sets the aggregator address correctly ", async () => { const response = await fundMe.priceFeed; assert.equal(response, mockV3Aggregator.address); }); }); describe("fund", () => { it("Fails if you dont send enough eth", async () => { await expect(fundMe.fund()).to.be.revertedWithCustomError( "You need to spend more ETH!" ); }); }); }); ``` and I get this error : ``` error FundMe Constructor ✔ Sets the aggregator address correctly fund 1) Fails if you dont send enough eth 1 passing (757ms) 1 failing 1) FundMe fund Fails if you dont send enough eth: TypeError: fundMe.fund is not a function at Context. (test/unit/FundMe.test.js:21:27) at processImmediate (node:internal/timers:471:21) error Command failed with exit code 1. ``` This is my Fundme contract ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.8; import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; import "./PriceConvertor.sol"; error NotOwner(); /** * @title A croud funding decentralized platform * @author Leo Franklin * @notice This implements price feed as a library and check with the real time value of currrency and able to donate the eth */ contract FundMe { using PriceConverter for uint256; mapping(address => uint256) public addressToAmountFunded; address[] public funders; address public immutable i_owner; uint256 public constant MINIMUM_USD = 50 * 10 ** 18; AggregatorV3Interface public priceFeed; constructor(address priceFeedAddress) { i_owner = msg.sender; priceFeed=AggregatorV3Interface(priceFeedAddress); } function fund() public payable { require(msg.value.getConversionRate(priceFeed) >= MINIMUM_USD, "You need to spend more ETH!"); // require(PriceConverter.getConversionRate(msg.value) >= MINIMUM_USD, "You need to spend more ETH!"); addressToAmountFunded[msg.sender] += msg.value; funders.push(msg.sender); } modifier onlyOwner { // require(msg.sender == owner); if (msg.sender != i_owner) revert NotOwner(); _; } function withdraw() public onlyOwner { for (uint256 funderIndex=0; funderIndex < funders.length; funderIndex++){ address funder = funders[funderIndex]; addressToAmountFunded[funder] = 0; } funders = new address[](0); (bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}(""); require(callSuccess, "Call failed"); } fallback() external payable { fund(); } receive() external payable { fund(); } } ```
nagrarohit commented 1 year ago

@LeoFranklin015 , hlo dear how are you? I hope you are doing fine . So now lets come to the issue. Look! Here is the problem , you are not awaiting the fundMe = ethers.getContract("FundMe", deployer); it should be like this:- fundMe = await ethers.getContract("FundMe", deployer); change it accordingly and thank me later.

nagrarohit commented 1 year ago

and change :- await expect(fundMe.fund()).to.be.revertedWithCustomError( "You need to spend more ETH!") with await expect(fundMe.fund()).to.be.revertedWith( "You need to spend more ETH!"