my fundMe test is failing #4578
-
when I test de fund function in the fundMe contract it saves double the aumont i send. I can't figure out whene it runs the fund function again. this is the fund me contract: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./PriceConverter.sol";
import "hardhat/console.sol";
error FundMe__NotOwner();
/**@title a contract for crowd funding
* @author La Bauchoneta
* @notice this contract is to demo a funding contract
* @dev this implements price feeds as our librery
*/
contract FundMe {
using PriceConverter for uint256;
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public immutable owner;
uint256 public constant MINIMUM_USD = 50 * 10 ** 18;
AggregatorV3Interface public priceFeed;
modifier onlyOwner() {
// require(msg.sender == i_owner);
if (msg.sender != owner) revert FundMe__NotOwner();
_;
}
constructor(address priceFeedAddress) {
owner = msg.sender;
priceFeed = AggregatorV3Interface(priceFeedAddress);
}
/**@notice this function funds the contract
* @dev this implements price feeds as our librery
*/
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!");
console.log("valor de msg.value: ", msg.value);
addressToAmountFunded[msg.sender] += msg.value;
funders.push(msg.sender);
}
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");
}
} this is the test: beforeEach(async () => {
deployer = (await getNamedAccounts()).deployer
await deployments.fixture(["all"])
fundMe = await ethers.getContract("FundMe", deployer)
mockV3Aggregator = await ethers.getContract(
"MockV3Aggregator",
deployer
)
})
describe("fund", async () => {
it("Updates the amount funded data structure", async () => {
await fundMe.fund({ value: sendValue })
const response = await fundMe.addressToAmountFunded(deployer)
assert.equal(response.toString(), sendValue.toString())
})
}) this is the log: FundMe
fund
valor de msg.value: 1000000000000000000
valor de msg.value: 1000000000000000000
1) Updates the amount funded data structure
0 passing (609ms)
1 failing
1) FundMe
fund
Updates the amount funded data structure:
AssertionError: expected '2000000000000000000' to equal '1000000000000000000'
+ expected - actual
-2000000000000000000
+1000000000000000000
at Context.<anonymous> (test/unit/FundMe.test.js:31:20)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at runNextTicks (node:internal/process/task_queues:64:3)
at listOnTimeout (node:internal/timers:533:9)
at processTimers (node:internal/timers:507:7)
error Command failed with exit code 1. |
Beta Was this translation helpful? Give feedback.
Answered by
alymurtazamemon
Jan 23, 2023
Replies: 1 comment 2 replies
-
@BauCalvo Your fund function is calling 2 times as you can see your console log statement prints 2 times. Maybe you have called the fund function somewhere else which is effecting this test. It would be better if you left your repo so we can find the issue. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
BauCalvo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@BauCalvo Your fund function is calling 2 times as you can see your console log statement prints 2 times. Maybe you have called the fund function somewhere else which is effecting this test. It would be better if you left your repo so we can find the issue.