4

I was going through a function that emits multiple events. Usualyy, if a function emits a single event, we can check that during the test cases using:

 await expect(contract.claimTokens(busd.address))
   .to.emit(contract, "Claimed")
   .withArgs(user.address, busd.address);

But what if a function has more than 1 event? How to compare that? Any answer will be appreciated.

3 Answers 3

5

I found the answer. You just have to append the next event in the same expect. Something like this:

await expect(contract.claimTokens(busd.address))
   .to.emit(contract, "Claimed")
   .withArgs(user.address, busd.address)
   .to.emit(contract, "Claimed")
   .withArgs(user.address, busd.address);
2

In my case, it didn't work. The event expected first wasn't correctly tested, I can put any value, and it returns always true.

I change it like this:

            const tx = contract.claimTokens(busd.address);
            await Promise.all([
                expect(tx).to.emit(contract, "Claimed").withArgs(user.address, busd.address),
                expect(tx).to.emit(contract, "Claimed").withArgs(user.address, busd.address)
            ])
0

You can also use .and infront to make it more readable

await expect(contract.claimTokens(busd.address))
   .to.emit(contract, "Claimed")
   .withArgs(user.address, busd.address)
   .and.to.emit(contract, "Claimed")
   .withArgs(user.address, busd.address);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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