[Ethereum] Making assertions (assert.equal) on two return values in Truffle

javascriptsoliditytruffle

Appreciate any help I can get. I am trying to make an assertion on returned values from a smart contract, and I cannot figure out how to get it to work for a Truffle test. I cannot show all my code in raw format because it is sensitive in nature, but here is what you need from the test:

it("should accept a list of obj's and return a string & addr", async () => {
    let cInstance = await Contract.deployed("tester2");
    let cAddress = cInstance.address;
    let returnResult = await cInstance.submission.call("tester2","0xdade","type2","0xwizards",[1,"2","4",4]);
    assert.equal(returnResult.valueOf(), "0xdade", {contractaddress});

Here is the function I'm calling on contract:

function submission(string memory Id, string memory vId, string memory entryType, string memory hashSig, uint256[] memory list) public returns(string memory, address) {

    return(submittedObjStructs[vId].vId, address(this))
}

The contract function returns a string and the contract address, which comes back in an object format.

When I try to use assert.equal(result.valueOf(), string value, address value) it doesn't work and gives me an error that the values don't match for various reasons. And if I try array notation like assert.equal(result[0].valueOf(), string value) I get a vm error and the transaction reverts. Any way I can compare the value of the object that comes back from the function call: {0: '0xdade', 1: '0xcontractaddressexample'}

Best Answer

When none of the returned values is an integer (such as in your case), you can use this:

const [x, y] = await cInstance.submission.call(...);
const expected = `0xdade, ${contractaddress}`;
const actual = `${x}, ${y}`;
assert.equal(actual, expected);

In other cases (that you may possibly have), for any integer value, add .toFixed().

For example, suppose that the first returned-value is uint256, then use this:

const actual = `${x.toFixed()}, ${y}`;
Related Topic