0

I have not been able to find any resources with usage instruction besides github and blog post by the author

On github an example is show as such:

truffleAssert.eventEmitted(result, 'TestEvent', (ev) => {
    return ev.param1 === 10 && ev.param2 === ev.param3;
});

And on the blogpost the syntax is like this:

truffleAssert.eventEmitted(tx, 'Play', (ev) => {
    return ev.player === bettingAccount && !ev.betNumber.eq(ev.winningNumber);
});

Both of these are rather similar.

This is the emit I am trying to test:

event Emittest(uint date, address indexed from, address indexed to, uint256 value);

My issues is that if I try to run the same format:

let emittest = await contract.function(accounts[1], 66500000,{from: accounts[0]});

truffleAssert.eventEmitted(emittest, 'Emittest', (ev) => {
        return ev.value == set_value;
});

It fails with the error AssertionError: Event filter for Transfer returned no results. I can extract the correct value by changing the code to:

truffleAssert.eventEmitted(emittest, 'Emittest', (ev) => {
        return ev.value.words[0] == set_value;
});

As the tokens is of the following structure:

value ==== BN {
  negative: 0,
  words: [ 60475904, 14, <1 empty item> ],
  length: 2,
  red: null
}   

Accessing .words[0] doesn't seem like the correct way as it makes it much more difficult to check BigNumbers which are split across multiple indices of the array words: without writing a helper function. Addresses extracted from the emit appear and can be accessed as expected. Splitting between multiple words happens much sooner than the 2^53 limit of javascript at around 66 million. I am new to javascript, and might be doing multiple things wrong. Thanks for help.

0

The problems is that you cannot compare objects with == (since numeric values in javascript do not have enough precision they are wrapped in an BN object).

So you have to use the methods provided by the BN library

return !ev.value.eq(tokens_sent);

The BN library has several methods besides .eq() take a look at their documentation.

For backward compatibility Truffle also supports BigNumber library, if for some strange reason BN's methods fail try with BigNumber's methods instead.

| improve this answer | |

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.