At present there is no way I can see or is documented to be able to easily use something like this with the truffle framework:

So using chai and writing a test to deploy a contract with something like this.

c = await MyContract.new();
// this wont work...
truffleAssert.eventEmitted(c, "ContractCreated", (ev) => {
    return ev.param === myInputParam;
});

My contract constructor will emit a ContractCreated event. I want to assert that this happened and the values are as expected. The event itself and its content is irrelevant. Its more the process of asserting events were emitted and contract construction time is important.

I would expect or want that I can do this assertion and use the assertEventEmitted function above.

Currently the only way I can see to accomplish this is so:

    let event = c.allEvents({fromBlock: 0, toBlock: 'latest'});
    event.watch(function (error, result) {
        if (!error){
            console.log("Result: ", result);
            // or some assertions similar to assertEventEmitted(...)
        }
    });

I can't find an elegant way to read the event logs emitted during contract construction using the truffle toolset.

When you send a transaction in you get the result object you can assert, upon contract creation the object returned is different and as such the logs are not available to check for event emission.

Does anyone know of a way to get those event logs from a truffle contract creation call and use the truffle-assertions.assertEventEmitted function to test with?

Thanks in Advance.

Having posted details of this issue to the github for truffle I got some really involved options outlined by the developers here: https://github.com/trufflesuite/truffle/issues/1196

The long and short of it is the simplest method is to do the method I showed above in a test scenario until V5 of Truffle comes out or you can move the the next release right now on NPM.

This will give you some excellent semantics to catch the tx receipt and logs in a callback at the end of the contract creation calls. They are using the newer Web3.js libraries that have a whole raft of optimisations and helper functions added to common calls.

From cgewecke:

Another thing you might be interested in is Truffle V5 (which is available in pre-release form from npm as truffle@next). It uses Web3 1.0 and lets you listen for transactionHash and receipt events per transaction. You could do something like:

MyContract
  .new()
  .onReceipt( receipt => {
     truffleAssert(..etc..)
     done(); // Tell mocha you finished
  });

Thanks https://github.com/cgewecke for all your help!

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.