FundMe Constructor Test Error #253
-
I am facing problem running Error
Contract// SPDX-License-Identifier: MIT
/** @title A Contract for Crowd Funding using Chainlink Feeds
@author Abhisek Yadav
@notice This is to demo a sample funding contract and beware it is not tested for commercial use
@dev This implements chainlink price feed as our library
*/
pragma solidity ^0.8.0;
import "./PriceConverter.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract Fundme {
using PriceConverter for uint256;
uint256 public constant MIN_USD = 50 * 1e18;
AggregatorV3Interface public s_priceFeed;
address[] public funders;
mapping(address => uint256) addressToFunds;
mapping(address => bool) addressToAdded;
address public immutable i_owner; //i_owner because it's immutable //it saves gas
constructor(address priceFeedAddress) {
i_owner = msg.sender;
s_priceFeed = AggregatorV3Interface(priceFeedAddress);
}
modifier onlyOwner() {
require(msg.sender == i_owner, "Sender is not owner");
_;
}
function fund() public payable {
require(
msg.value.getConversionRate(s_priceFeed) >= MIN_USD,
"Not enough USD Sent"
);
if (!addressToAdded[msg.sender]) {
funders.push(msg.sender);
}
addressToFunds[msg.sender] = msg.value;
addressToAdded[msg.sender] = true;
}
function withdraw() public onlyOwner {
for (uint256 i = 0; i < funders.length; i++) {
address funder = funders[i];
addressToFunds[funder] = 0;
}
//resetting the array
funders = new address[](0);
//Three Different ways to withdraw funds
//transfer,send and call
// payable(msg.sender).transfer(address(this).balance);
(bool callSuccess, ) = payable(msg.sender).call{
value: address(this).balance
}("");
require(callSuccess, "call failed");
}
receive() external payable {
fund();
}
fallback() external payable {
fund();
}
function getPriceFeed() public view returns (AggregatorV3Interface) {
return s_priceFeed;
}
} Testconst { deployments, ethers, getNamedAccounts } = require("hardhat");
const { assert } = require("chai");
describe("Fundme", async () => {
let fundMe, deployer, mockV3Aggregator;
beforeEach(async () => {
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]);
fundMe = await deployments.get("Fundme", deployer);
mockV3Aggregator = await deployments.get("MockV3Aggregator", deployer);
});
describe("constructor", function () {
it("Sets the aggregator address correctly in the contractor", async () => {
const response = await fundMe.getPriceFeed();
assert.equal(response, mockV3Aggregator.address);
});
});
}); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hi! In your test: remove "get" from here: |
Beta Was this translation helpful? Give feedback.
-
Try this in your test: fundMe = await ethers.getContract("Fundme", deployer); Instead of what you have. |
Beta Was this translation helpful? Give feedback.
-
I am facing this error below:
Here is my FundMe.test.js: const { deployments, ethers, getNamedAccounts } = require("hardhat");
const { assert } = require("chai");
describe("FundMe", async () => {
let fundMe, deployer, mockV3Aggregator;
beforeEach(async () => {
// Another way to get accounts is:
// const accounts = await ethers.getSigners();
// const account = accounts[0];
// We are first going to deploy our FundMe contract using Hardhat-deploy
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]);
fundMe = await ethers.getContract("FundMe", deployer);
mockV3Aggregator = await ethers.getContract(
"MockV3Aggregator",
deployer
);
});
describe("constructor", async () => {
it("sets the aggregator address correctly", async () => {
const response = await fundMe.priceFeed();
assert.equal(response, mockV3Aggregator.address);
});
});
}); How do I resolve the error please? |
Beta Was this translation helpful? Give feedback.
deployments.get
returns adeployment
objectethers.getContract
returns acontract
object <- This is the one you want!Try this in your test:
Instead of what you have.