1

I have written an unit test but when I launch it I get following error:

Test.sol:25:22: TypeError:
 Member "val1" not found or not visible after argument-dependent lookup in function () view external returns (string s1, string s2, uint256 val1 memory,uint256,uint256,uint256,uint256,address).
        Assert.equal(partProductionContract.production.val1, _val1, "msg.sender: ");
                     ^------------------------------------------^
Compilation failed. See above.
Truffle v5.0.1 (core: 5.0.1)
Node v11.6.0

I don know what could be te problem, so I post some pieces of the smart contract and of the test function:

contract PartProduction {

    struct Production {
        string s1;
        string s2;
        uint256 val1;
    }

    Production public production;

    constructor(
        string s1,
        string s2,
        string val1,
    )public {
        production.s1 = s1;
        production.s2 = s2;
        production.val1 = _val1;
    }
}

This is the unit test function:

contract Test {
    // The address of the adoption contract to be tested
    PartProduction public partProductionContract = PartProduction(DeployedAddresses.PartProduction());

    function testSetParameters() public {
      string memory _val1 = "someString";
        Assert.equal(partProductionContract.production.val1, _val1, "msg.sender: ");
    }

}

1 Answer 1

1

In solidity you do not have direct access to a different contract properties even if they are public. You have to explicitely call the getter function partProductionContract.production().

But since you are using a struct and the test contract doesn't known the struct that will fail. You have to somehow share the definition of the struct between your contract and the test contract.

Also you have another problem with structs. Returning structs is an experimental feature of solidity. You need to enable ABIEncoderV2 and create your own getter.

pragma solidity ^0.5.2;
pragma experimental ABIEncoderV2;   // required to return structs in a public function

contract PartProductionContract {
    // Default getter returns a tuple
    Production public production;

    // We have to create our own getter
    function production2() public returns (Production memory) {
        return production;
    }
}


// Test inherit from PartProduction so both share the definition of Production
contract Test is PartProduction {

    function testSetParameters() public {
        // We have to call our own getter
        Production memory prod = partProductionContract.production2();
        string memory _val1 = "someString";
        Assert.equal(prod.val1, _val1, "msg.sender: ");
    }
}

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.