Unit test on remix keeps throwing typeError

remixremix-testssolidity

I am trying to write a unit test for my token contract whose constructor takes 6 arguments. I have written the test but each time I try to test any function on the token contract it throws TypeError saying "Member "[functionName]" not found or not visible after argument-dependent lookup in type".
Here's the unit test code

// 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 "./Bet.sol";

contract testSuite is Bet{
    constructor() Bet(TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0)) payable {
        
    }


     /// 'beforeAll' runs before all other tests
    /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
    function beforeAll() public { 
        Assert.equal(testSuite.totalSupply(), 250000000 * 10 ** 8, "Total supply should be 250 
           million");
    }
}

Please, any help on how I can resolve this?

Best Answer

Proper approach to the unit testing is test contract with external calls, in this way you can see the call parameters and return values then decide what structure you should be using. If you want to test internal/private functions, make them public just for unit testing, after verifying your code make them internal/private. I think you'll create confusion for yourself when inheriting your tester code from your tested code, it'll be hard to maintain in future. I'm not speaking just for Solidity, but this holds for other languages too.

I have tried to recreate your scenario below

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

// TESTED Contract
contract Bet{
    // For convenience
    event Hello(
      address acc0, address acc1, address acc2,
      address acc3,address acc4, address acc5);
        
    constructor(
        address acc0, 
        address acc1,
        address acc2,
        address acc3,
        address acc4,
        address acc5
    ) payable {
        emit Hello(acc0, acc1, acc2, acc3, acc4,acc5);
    }
    
    function totalSupply() public pure returns (uint256)
    {
        return 250000000 * 10 ** 8;
    }

}

The tester contract is:

// 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 "./Bet.sol";

// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract testSuite {
    
    // Instance of the tested contract
    Bet bet = new Bet( 0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
        0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
        0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
        0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
        0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
        0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB
        );

    function beforeAll() public {
        Assert.equal(bet.totalSupply(), 250000000 * 10 ** 8, "Total supply should be 250 million");
    }

    function checkFailure() public {
        Assert.notEqual(bet.totalSupply(), 250000000 * 10 ** 7, "Total supply should not be 250 thousand");
    }
}
Related Topic