5

Good day,

I'm using mochajs to test my Solidity code using truffle and testrpc.

In my contract I got the following event and function:

contract store{
    event MoneyIn (address _from, uint256 _amount);

    function getMoney() payable{
        //event
        MoneyIn(msg.sender, msg.value);
    } 
}

And this is the mochajs code:

it("Pay to store", function(){
    return storeContract.getMoney({from: account[0], value: txAmount}).then(function(res){
      assert.isDefined(res);
    });
});

It simpally checks if the transaction was executed by checking if the res is defined.

However, when mocha don't throw errors, it also won't emit the event. The only way in which I can get see my event is my creating an error: for example I can change the assertion from isDefined(res) to ifError(res).

Doing so will force mocha to throw an error and as a side product it will also emit my event:

1) Pay to store

Events emitted during test:
---------------------------

MoneyIn(_to: 0xa86120c19324a9ed51fe17730fce24edd69ddc9c, _amount: 300000000000000000)

---------------------------

I'll hihgly appriciate any tip you might have on how to force mocha to emit events even when there're no errors.

5

Explanation:

This functionality is not available in Truffle at the moment, as logged events are not displayed on passing tests (source code).

I like the idea of tracking events on passing tests as well, and it seems Truffle will eventually have this feature (tracking issue).

Workaround:

One way to show the events is to simply watch them as per the web3 API, and do a console.log (see the example code I have linked).

  • I did use console.log for some of my tests, but as they're usually presented at the TOP of the current execution, I decided against (It was really hard to follow them in the right order). I'm somewhat glad to know that currently, that's just the way it works. I spent days trying to understand what I'm doing wrong. – shultz Apr 30 '17 at 15:30
  • You could try something like this. This is in embark, but it also uses mocha, so this sort of thing should work out of the box for making assertions on events from contracts. getEvent itself is pretty straightforward – DeviateFish May 1 '17 at 8:27
0

In web3.js version 1 the solution is similar to what @travis-jacobs proposes:

it('school "AtAddress" event was emitted and equal to call result', async () => {
    const events = await school.getPastEvents('AtAdress');
    const addressFromEvent = events[0].returnValues['loc'];
    assert.equal(courseAddress, addressFromEvent);
});

See a complete example here.

Note that web3-beta-35 and below can't handle events with indexed parameters.

0

In addition to the other answers, my truffle-assertions library also offers the ability to log all emitted events inside a transaction in the same way as truffle outputs them on error.

The library can be installed through npm

npm install truffle-assertions

Then, it can be imported at the top of your test file

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

And finally it can be used to output all emitted events inside a transaction.

let result = storeContract.getMoney({from: account[0], value: txAmount});
truffleAssert.prettyPrintEmittedEvents(result);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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