truffle-assertions
truffle-assertions copied to clipboard
Change the logic so that a "return" is optional in the "eventEmitted".
Currently I just use the default assertions to check the properties + values from an event:
truffleAssert.eventEmitted(result, 'AddSupplierEvent', (ev) => {
assert.isNotEmpty(ev.from);
assert.equal(ev.id, supplier1);
assert.equal(ev.name, 'Supplier 1');
return true;
});
What I really would like to write is:
truffleAssert.eventEmitted(result, 'AddSupplierEvent', (ev) => {
assert.isNotEmpty(ev.from);
assert.equal(ev.id, supplier1);
assert.equal(ev.name, 'Supplier 1');
});
Is this possible with a code change?
Hi @StefH
Thanks for opening this issue.
So the way I envisioned the library to work is by using a sort of 'filter' function on the arguments, so for your use case this would be like I also posted on your stackexchange question (https://ethereum.stackexchange.com/questions/64137/solidity-coverage-adds-extra-events-which-make-unit-test-fail)
truffleAssert.eventEmitted(result, 'AddSupplierEvent', (ev) => {
return !!ev.from && ev.id === supplier1 && ev.name === 'Supplier 1';
});
However, I've seen multiple people using it in the way you're describing (asserting inside the function), so I'm definitely willing to make some changes to accommodate both ways.
What is the reason you're using the function that way, is it because you want to get a separate assertion failure for the specific argument that is incorrect, or is there another reason for it?
The reason is that it just feels better to use the asserts. And more functionality is provided out of the box by assert.
Maybe a new function name could be assertEmittedEvent
?
truffleAssert.assertEmittedEvent(result, 'AddSupplierEvent', (ev) => {
assert.isNotEmpty(ev.from);
assert.equal(ev.id, supplier1);
assert.equal(ev.name, 'Supplier 1');
});
I'll add, and maybe there's a better approach (let me know if so! all ears), but I am currently sometimes using this to extract values from the event and use them later in the test. For instance:
var name;
truffleAssert.assertEmittedEvent(result, 'AddSupplierEvent', (ev) => {
assert.isNotEmpty(ev.from);
assert.equal(ev.id, supplier1);
assert.equal(ev.name, 'Supplier 1');
name=ev.name;
});
contract.supplierMap.call(name) (etc.)
I'm still pretty new to the space overall, but as a heads up this is a use case I've had for this library, and extracting out the fields doesn't require the true/false. Again though, if there's a more recommended approach to extracting values let me know : ).