Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support expectEvent testing for multiple emits of the same event in a transaction #135

Open
abcoathup opened this issue Oct 26, 2020 · 9 comments

Comments

@abcoathup
Copy link
Contributor

Support expectEvent testing for multiple emits.

Currently expectEvent will pass for 1 or more emits of the event.

Raised by a community member in Telegram:
https://forum.openzeppelin.com/t/test-for-multiple-emits-of-the-same-event-with-expectevent/4106

@fabdarice
Copy link

Also looking for support for this feature.
Thanks

@frangio
Copy link
Contributor

frangio commented Apr 9, 2021

Would something like the following work, with a new options argument specifying the times the event should show up.

expectEvent(receipt, 'Foo', { arg1: 'bar' }, { times: 2 });

And this function would return the array of found events.

@fab-spring
Copy link

Not really, events could be emitted many times but with different values

@frangio
Copy link
Contributor

frangio commented Apr 9, 2021

In that case you can use subsequent calls to expectEvent.

expectEvent(receipt, 'Foo', { arg1: 'bar' });
expectEvent(receipt, 'Foo', { arg1: 'quux' });

Otherwise I'm not sure I'm understanding the request. Can someone provide an example of what they would like to write and how they would want it to behave?

@SSharshunov
Copy link

SSharshunov commented Aug 14, 2021

For example:
my contract loops through the conditions, and each time generates the same event, but for example the result changes

@SSharshunov
Copy link

SSharshunov commented Aug 14, 2021

if (x == 1 || x == 2) {
    uint y = doCalc(some1, some0);
    emit yCalculated(x, y); // Triggering event
    if (x == 2) {
        uint y2 = doCalc(some0, some1);
        emit yCalculated(x, y2); // Triggering event
    }
}

@frangio
Copy link
Contributor

frangio commented Aug 17, 2021

@SSharshunov In this you would also want to check that the events are emitted in the correct order?

@zikyfranky
Copy link

if (x == 1 || x == 2) {
    uint y = doCalc(some1, some0);
    emit yCalculated(x, y); // Triggering event
    if (x == 2) {
        uint y2 = doCalc(some0, some1);
        emit yCalculated(x, y2); // Triggering event
    }
}

Just call the expectEvent and pass the right event name and the values of said events

If you are expecting 3 events of Transfer

expecteEvent(tx, 'Transfer', {from:0x.., to:0x.., value:value}
expecteEvent(tx, 'Transfer', {from:0x.., to:0x.., value:value}
expecteEvent(tx, 'Transfer', {from:0x.., to:0x.., value:value}

The value MUST be of type BigNumber or a String, else expectEvent would give you a misleading error expected {expected} but got {actual} cause it only throws the very first error in the error array, which isn't the actual error.
expectEvent Source
I had to go into the library function to find this.

@RUIRUI-ashui
Copy link

Does anyone know how to use it for multiple events in one function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants