Cyfrin / foundry-full-course-cu

GNU General Public License v3.0
2.98k stars 694 forks source link

Reason: Assertion failed.] testFailWithoutEnoughEth() (gas: 22948) #157

Closed deep0072 closed 1 year ago

deep0072 commented 1 year ago

Lesson

Lesson 7

https://www.youtube.com/watch?v=sas02qSFZ74)

No response

Operating System

Windows

Describe the bug

here i am trying to run test using forge test.


fund me.sol
function fund() public payable {
        if (msg.value.conversionRate(s_priceFeed) < MINIMUM_USD) {
            revert Not_Sufficient_Amount();
        }```

test.sol
```   function testFailWithoutEnoughEth() public {
        // this test supposed to be passed when we don't have enough eth to
        vm.expectRevert(); // the next line should revert
        fundMe.fund(); // here we are trying to send the zero eth to
    }```

this test should be passed as i am using vm.expectRevert() here why this test failing
ChefAharoni commented 1 year ago

Problems and bugs should be opened in the discussion part; this section is for issues in the course / repo / etc..

In any case, have you tried prompting ChatGPT or any other AI for this problem? This is the prompt I got when copied your issue to it, this might help:

If your test is failing, it's possible that the fundMe.fund() method is not actually triggering the revert Not_Sufficient_Amount(); as expected.

There could be several reasons why your test is failing:

  1. There is a sufficient amount of Ether: As per the condition in your contract, if msg.value.conversionRate(s_priceFeed) is greater than or equal to MINIMUM_USD, then the contract will not revert. Please ensure that when you call fundMe.fund() in your test, you are not sending any Ether or the conversion rate results in a value that is less than MINIMUM_USD.

  2. Price feed data issue: If s_priceFeed doesn't return the expected value, the condition may not evaluate as expected. Make sure the oracle data feed is providing the correct value.

  3. Error in expectRevert usage: Ensure you're using the expectRevert correctly. The correct syntax as per OpenZeppelin test helpers is await expectRevert.unspecified(yourFunctionCall). It could look like:

    await expectRevert.unspecified(fundMe.fund());
  4. Mismatch in revert reason: If the revert reason in your test doesn't match the revert reason in your smart contract, the test will fail. If the contract code is using a custom error type like revert Not_Sufficient_Amount(); then you should specify this in your test code as follows:

await expectRevert(fundMe.fund(), "Not_Sufficient_Amount");

Without knowing more details about your fundMe.fund() implementation and the context, it's hard to give a more accurate answer. Ensure to check the aforementioned points in order to troubleshoot the issue.

deep0072 commented 1 year ago

msg.value.conversionRate(s_priceFeed) here msg.value is zero then how can be it greater than zero i have used phind.com as well. but still issue not resolved

deep0072 commented 1 year ago

fix the issue by changing function name function testFailWithoutEnoughEth() to function testFundFailsWithoutEnoughETH()

but dont know why it fixed. anyone can explain

DevSwayam commented 1 year ago

Could you please provide the whole contract with snapshot of error?. Additionally when your using

 function testFundFailsWithoutEnoughETH() 

i guess test is not able to find function cause solidity is case sensitive hence it is passing idk. But could you share this in Discussion tab + and provide source code?

DCVglobalnetwork commented 2 months ago

Hi Could you help me with this

image