0

I am trying to test two SCs on Remix: //F5.sol

pragma solidity ^0.5.3;
contract F5{
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}

//STFB5.sol

pragma solidity ^0.5.3;
contract STFB5{
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
 }

==STFB5_test.sol The test file is given below:

pragma solidity >=0.4.22 <0.9.0;

// This import is automatically injected by Remix
import "remix_tests.sol"; 

// This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol";
import "./STFB5.sol";
import "./F5.sol";
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts

contract testSuite {

    /// 'beforeAll' runs before all other tests
    /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
    F5 obj1;
    STFB5 obj2;
    function beforeAll() public {
        // <instantiate contract>
       obj1 = new F5();
       obj2 = new STFB5();
    }
    function initialValueofObj1ShouldBe100() public returns (bool) {
       return Assert.equal(obj1.getBalance(), 100, "initial value is not correct");
    }
    function initialValueofObj2ShouldBe100() public returns (bool) {
       return Assert.equal(obj2.getBalance(), 100, "initial value is not correct");
    }
    
}

When I deployed the contracts, I got following addresses:

0xd9145CCE52D386f254917e481eB44e9943F39138=F5.sol 0xD7ACd2a9FD159E69Bb102A1ca21C9a3e3A5F771B=STFB5.sol

But in the attached image the ACCOUNT text box shows the address which does not match with the above address. I also used the up and down arrows of ACCOUNT text box but can’t locate the above addresses.

When I ran the tests, both the assert statements failed, I got the following output:

Progress: 1 finished (of 1)
FAIL testSuite (contracts/STFB5_test.sol)
✘ Initial valueof obj1 should be100
Error Message:
"initial value is not correct"
Assertion:
Expected value should be
equal
to 100
Received value:
0
Skipping the remaining tests of the function.
✘ Initial valueof obj2 should be100
Error Message:
"initial value is not correct"
Assertion:
Expected value should be
equal
to 100
Received value:
0
Skipping the remaining tests of the function.
Result for contracts/STFB5_test.sol
Failing: 2
Total time: 0.17s

Somebody please guide me.

Zulfi.Image of Remix IDE, Problem with Account textbox ==Updated 12-23-2021 STFB5.sol

pragma solidity ^0.5.3;
contract STFB5 {
    function transferToFallback(address payable _to) public payable {
        _to.transfer(msg.value);
    }
    function() external payable{
    }
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
    constructor () payable public{
    }
}

F5.sol

pragma solidity ^0.5.3;

contract F5 {
        function() external payable {
        }
        constructor () payable public {}
        function getBalance() public view returns (uint) {
          return address(this).balance;
        }
}

STFB5_test.sol

// SPDX-License-Identifier: GPL-3.0
    
pragma solidity >=0.4.22 <0.9.0;

// This import is automatically injected by Remix
import "remix_tests.sol"; 

// This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol";
import "./STFB5.sol";
import "./F5.sol";
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts

contract testSuite {

    /// 'beforeAll' runs before all other tests
    /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
    F5 obj1;
    STFB5 obj2;

    function beforeAll() public payable {
        
       uint a = 100;
       obj1 = new F5{value: a}();
       obj2 = new STFB5{value: a}();


    }
    function initialValueofObj1ShouldBe100() public returns (bool) {
       return Assert.equal(obj1.getBalance(), 100, "initial value is not correct");
    }
    function initialValueofObj2ShouldBe100() public returns (bool) {
       return Assert.equal(obj2.getBalance(), 100, "initial value is not correct");
    }
}

But I am getting the error:

contracts/STFB5_test.sol:28:21: ParserError: Expected ';' but got '{'
obj1 = new F5{value: a}();
^

1 Answer 1

1

Accounts and contracts are not the same. Accounts are wallets of the users, while contracts are, well, contracts. When you deploy the contract the new unique address is generated for that contract. To know the explicit difference between the contract and the address please refer to The Documentation

Your tests fail because the contracts have their own balances and they are 0 initially. You can transfer value to the contract on deployment by specifying it in the Value textbox above the "Deploy" button. It is measured in Wei. In your contracts you are using new keyword to create new contracts.

To be able to transfer value to the contracts on deployment using new keyword you need to first deploy the testSuite contract with value in it. Then you can transfer value to the new contracts by specifying the value in {} like this:

       uint a = 100;
       obj1 = new F5{value: a}();
       obj2 = new STFB5{value: a}();

For the contracts to be able to receive Ether you need to use keyword payable. In each of the contracts create a payable constructor: constructor() payable {}. Then Add payable to the function beforeAll(): function beforeAll() public payable {

This should be it! Let me know if there are any problems.

6
  • Hi my friend, thanks for helping. I am still getting the error contracts/STFB5_test.sol:28:21: ParserError: Expected ';' but got '{' obj1 = new F5{value: a}(); Kindly see all information in my updated main post. Zulfi.
    – zak100
    Commented Dec 24, 2021 at 4:09
  • 1
    I can't replicate the problem, there are not PerserErrors on my end. In the meantime, I do see some of the mistakes in the contracts. Make sure to add constructor() payable {} in the testSuite contract. Remove function() external payable {} from STFB5 and F5 contracts as it is not valid Solidity. Make sure you send Wei with your contract deployment. Commented Dec 24, 2021 at 5:58
  • Hi, kindly provide link of documentation/example from where you got the commands:obj1 = new F5{value: a}(); and obj2 = new STFB5{value: a}(); I can't find an example of invoking constructor in this way in Solidity. Zulfi.
    – zak100
    Commented Dec 24, 2021 at 16:09
  • 1
    You would want to see the use of {value: x} by this link docs.soliditylang.org/en/v0.8.11/… And the use of it with the constructor by this link docs.soliditylang.org/en/v0.8.11/… Commented Dec 24, 2021 at 19:02
  • 1
    As per the documentation, "Before Solidity 0.6.2, the recommended way to specify the value and gas was to use f.value(x).gas(g)(). This was deprecated in Solidity 0.6.2 and is no longer possible since Solidity 0.7.0." so in your case that your be obj1 = new F5.value(a)(); Commented Dec 25, 2021 at 0:53

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.