Added Test, Before Add Debug Capabilities
This commit is contained in:
parent
fb89cb6e1e
commit
b7dcbb3039
10
package.json
10
package.json
@ -29,12 +29,12 @@
|
||||
"hardhat": "^2.8.3",
|
||||
"hardhat-deploy": "^0.9.29",
|
||||
"hardhat-gas-reporter": "^1.0.7",
|
||||
"solidity-coverage": "^0.7.18",
|
||||
"solidity-coverage": "^0.8.12",
|
||||
"@chainlink/contracts": "^0.3.1",
|
||||
"dotenv": "^14.2.0",
|
||||
"prettier-plugin-solidity": "^1.0.0-beta.19",
|
||||
"solhint": "^3.3.7"
|
||||
|
||||
|
||||
|
||||
"solhint": "^5.0.2",
|
||||
"prettier": "^3.3.3",
|
||||
"prettier-plugin-solidity": "^1.3.1"
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
const { task } = require("hardhat/config");
|
||||
|
||||
task("block-number", "Prints current block number").setAction(
|
||||
task("sample-task", "Prints current block number").setAction(
|
||||
async (taskArgs, hre) => {
|
||||
const blockNumber = await hre.ethers.provider.getBlockNumber();
|
||||
console.log("Current block number: " + blockNumber);
|
||||
|
@ -1,3 +1,4 @@
|
||||
// yarn hardhat test
|
||||
const { ethers } = require("hardhat");
|
||||
const { expect, assert } = require("chai");
|
||||
|
149
test/unit/FundMe.test.js
Normal file
149
test/unit/FundMe.test.js
Normal file
@ -0,0 +1,149 @@
|
||||
const { ethers, deployments, getNamedAccounts } = require("hardhat");
|
||||
const { expect, assert } = require("chai");
|
||||
|
||||
describe("FundMe", function () {
|
||||
//// Test For The Constructor
|
||||
let fundMe;
|
||||
let deployer;
|
||||
let mockV3Aggregator;
|
||||
beforeEach(async () => {
|
||||
deployer = (await getNamedAccounts()).deployer;
|
||||
/* // Alternative To Above, Localhost will get 10 fake accounts
|
||||
const accounts = await ethers.getSigners();
|
||||
const account1 = accounts[0];
|
||||
const account2 = accounts[1];
|
||||
*/
|
||||
await deployments.fixture(["all"]);
|
||||
fundMe = await ethers.getContract("FundMe", deployer);
|
||||
mockV3Aggregator = await ethers.getContract(
|
||||
"MockV3Aggregator",
|
||||
deployer,
|
||||
);
|
||||
});
|
||||
|
||||
let eth1 = "1000000000000000000"; // 1 ETH
|
||||
eth1 = ethers.utils.parseEther("1"); // Same 1 ETH
|
||||
describe("constructor", async function () {
|
||||
it("Set the aggregator priceFeedAddress correctly", async function () {
|
||||
const response = await fundMe.priceFeed(); // AggregatorV3Interface
|
||||
//console.log("AggregatorV3Interface" + response);
|
||||
assert.equal(response, mockV3Aggregator.address);
|
||||
});
|
||||
it("Updated the amount funded data structure", async function () {
|
||||
await fundMe.fund({ value: eth1 });
|
||||
const response = await fundMe.addressToAmountFunded(deployer);
|
||||
assert.equal(response.toString(), eth1.toString()); // Send 1 ETH
|
||||
});
|
||||
it("Adds funder to array of funders", async function () {
|
||||
await fundMe.fund({ value: eth1 });
|
||||
const funder = await fundMe.funders(0);
|
||||
assert.equal(funder, deployer);
|
||||
});
|
||||
});
|
||||
|
||||
// Test for the fund() function
|
||||
describe("fund function", async function () {
|
||||
it("Fail if you don't send enough ETH", async function () {
|
||||
await expect(fundMe.fund()).to.be.revertedWith(
|
||||
"Didn't send enough",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// Test For The receive() function
|
||||
let FundMe;
|
||||
let owner;
|
||||
let addr1;
|
||||
let addr2;
|
||||
let addrs;
|
||||
beforeEach(async function () {
|
||||
// Get the ContractFactory and Signers here.
|
||||
[owner, addr1, addr2, ...addrs] = await ethers.getSigners();
|
||||
|
||||
// Deploy Mock Price Feed
|
||||
const MockV3Aggregator =
|
||||
await ethers.getContractFactory("MockV3Aggregator");
|
||||
mockV3Aggregator = await MockV3Aggregator.deploy(8, 2000 * 1e8);
|
||||
|
||||
// Deploy FundMe contract
|
||||
FundMe = await ethers.getContractFactory("FundMe");
|
||||
fundMe = await FundMe.deploy(mockV3Aggregator.address);
|
||||
await fundMe.deployed();
|
||||
});
|
||||
|
||||
describe("receive function", function () {
|
||||
it("should call the fund function when receiving Ether", async function () {
|
||||
// Send ETH to the contract
|
||||
await addr1.sendTransaction({
|
||||
to: fundMe.address,
|
||||
value: ethers.utils.parseEther("1.0"), // 1 ETH
|
||||
});
|
||||
|
||||
// Check if addr1 is added to funders
|
||||
expect(await fundMe.funders(0)).to.equal(addr1.address);
|
||||
// Check if the amount funded is recorded
|
||||
expect(await fundMe.addressToAmountFunded(addr1.address)).to.equal(
|
||||
ethers.utils.parseEther("1.0"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should revert if the amount sent is below the minimum funding value", async function () {
|
||||
// 1 USD = 0.0005 ETH (Assuming 1 ETH = 2000 USD)
|
||||
const minFunding = ethers.utils.parseEther("0.0005"); // 0.5 USD
|
||||
|
||||
await expect(
|
||||
addr1.sendTransaction({
|
||||
to: fundMe.address,
|
||||
value: minFunding.sub(1), // Slightly less than the minimum
|
||||
}),
|
||||
).to.be.revertedWith("Didn't send enough");
|
||||
});
|
||||
|
||||
it("should accept funds greater than or equal to the minimum funding value", async function () {
|
||||
// 50 USD = 0.025 ETH (Assuming 1 ETH = 2000 USD)
|
||||
const minFunding = ethers.utils.parseEther("0.025");
|
||||
|
||||
await expect(
|
||||
addr1.sendTransaction({
|
||||
to: fundMe.address,
|
||||
value: minFunding, // Exactly the minimum
|
||||
}),
|
||||
).not.to.be.reverted;
|
||||
|
||||
expect(await fundMe.funders(0)).to.equal(addr1.address);
|
||||
expect(await fundMe.addressToAmountFunded(addr1.address)).to.equal(
|
||||
minFunding,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("withdraw", async function () {
|
||||
beforeEach(async function () {
|
||||
await fundMe.fund({ value: eth1 }); // Add 1 ETH first
|
||||
});
|
||||
it("Withdraw ETH from a single founder", async function () {
|
||||
// Arrange
|
||||
const startingFundMeBalance = await fundMe.provider.getBalance(
|
||||
fundMe.address,
|
||||
);
|
||||
const startingDeployerBalance =
|
||||
await fundMe.provider.getBalance(deployer);
|
||||
|
||||
// Act
|
||||
const transactionResponse = await fundMe.withdraw();
|
||||
const transactionReceipt = await transactionResponse.wait(1);
|
||||
|
||||
const endingFundMeBalance = await fundMe.provider.getBalance(
|
||||
fundMe.address,
|
||||
);
|
||||
const endingDeployerBalance =
|
||||
await fundMe.provider.getBalance(deployer);
|
||||
|
||||
//let gasCost = transactionReceipt;
|
||||
|
||||
// Assert
|
||||
assert.equal(endingFundMeBalance, 0);
|
||||
//assert.equal(startingFundMeBalance.add(startingDeployerBalance), endingDeployerBalance.add(gasCost).toString())
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user