0

In my smart contract, I have a function that sends ether to the caller using the msg.sender.send() function. I need to check that the value being returned by the function is correct (i.e. the amount is being calculated as expected). How can I do this in a truffle javascript test? I am managing to call the function etc, but I cannot seem to be able to read the value received (I need to replace the result.value call in the assertation below with the correct way to read the value received).

it('Check value received', function () {
    let meta;
    return MyContract.deployed().then(function (instance) {
        meta = instance;
        return meta.claimBalance(1, { from: accounts[0], gas: 1000000 });
    }).then(function (result) {
        assert.equal(expectedValue, result.value, "Incorrect value received");
    })
})
1

What you can do is emit an event with the values and read that in your Javascript test.

So you would add this to your solidity code (i.e. you emit an event with the return value inside the claimBalance function):

...
event BalanceClaimed(uint value);
...
function claimBalance(uint arg) public {
    ...
    emit BalanceClaimed(value);
    ...
}

Then you can use my truffle-assertions library to assert that the event was emitted.

You can install the library with npm

npm install truffle-assertions

Then you can import it at the top of your test file

const truffleAssert = require('truffle-assertions');

And you can change your Javascript test to the following:

it('Check value received', function () {
    let meta;
    return MyContract.deployed().then(function (instance) {
        meta = instance;
        return meta.claimBalance(1, { from: accounts[0], gas: 1000000 });
    }).then(function (result) {
        truffleAssert.eventEmitted(result, 'BalanceClaimed', (ev) => {
            return ev.value === expectedValue;
        }, 'Incorrect value received');
    })
})

Or using async/await:

 it('Check value received', async function () {
    const meta = await MyContract.deployed();
    const result = await meta.claimBalance(1, { from: accounts[0], gas: 1000000 });
    truffleAssert.eventEmitted(result, 'BalanceClaimed', (ev) => {
        return ev.value === expectedValue;
    }, 'Incorrect value received');
})
  • Thanks for the suggestion! So there is no way to read the message value received directly when you call a function? – user1365247 Jan 29 at 10:08
  • 1
    There's two different ways of calling functions, using 'call' and using 'sendTransaction'. A call only reads the state of the blockchain, and then you would be able to read the return value. However, you are also modifying the state of the blockchain (since you're sending funds), so then you automatically use the 'sendTransaction' method. Then you can't retrieve the return value fo the function, so events is the best alternative. – Rosco Kalis Jan 29 at 10:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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