1

I am using: https://remix.ethereum.org

contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Receiver {

    receive() external payable {}

    function getMoney() public payable {}

}

tests

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../contracts/Receiver.sol"; // Import the EventTicketing contract
import "remix_tests.sol"; 

contract TestReceiveEtherExample {
    Receiver public instance;

    function beforeEach() public {
        instance = new Receiver();
    }

    function checkBalance() public {
        uint256 balance = address(instance).balance;
        Assert.equal(balance, 0, "Initial balance should be 0");
    }

    function testReceiveEther() public payable {
        // Send 1 ether to the contract
        payable(address(instance)).transfer(1 ether);

        // Check if balance is updated correctly
        uint256 balance = address(instance).balance;
        Assert.equal(balance, 1 ether, "Balance should be 1 ether after receiving");
    }

    function testGetMoney() public payable {
        uint256 initialBalance = address(instance).balance;

        // Call the getMoney function
        instance.getMoney{value: 1 ether}();

        uint256 finalBalance = address(instance).balance;

        // Check if the balance has increased by 1 ether
        Assert.equal(finalBalance, initialBalance + 1 ether, "Balance should be increased by 1 ether after calling getMoney");
    }
}

result of running tests: tests_outpyut

calling functions directly on deployed smart contract works perfectly fine: enter image description here

1 Answer 1

1

Did you send value when deploying the contract? Here is a picture of where you need to include the value when deploying:

enter image description here

1
  • yes, calling contract directly works perfectly fine (as I described in the main post) only when running unit tests - some tests are failing (some, not all)
    – Harry
    Sep 16, 2023 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.