-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat(cheatcodes): add negative assertions for expectEmit
and expectRevert
using count
syntax
#509
Comments
Good idea. Does any other project do it, so that we can have some example? What would be your ideal syntax? |
I like this idea, agreed it'd be useful, but I don't have any examples offhand. It's easy to extend DSTest to add an It would also be useful to test that you don't jump to a certain function within your contract (and similarly have an Some syntax ideas:
There's probably another question of how topics should be considered when expecting an event not to be emitted. I'd guess most or all use cases only are really interested in only ensuring that topic0 is not found, as opposed to ensuring topic0 is emitted but that topic1/data does not have some value. The easiest behavior is to just invert the result of |
@mds1 Agreed wrt Slightly off topic, but for events in particular, wouldn't it make sense to also have a more ergonomic version that just tests for topic0 via |
That'd be a useful overload here, because then we can shorten it to s single line |
+1 for vm.not before expectNotEmit. For some protocols (like Nomad), where the functionality is heavily based on event emission, some times there are no other signals (or variables to be read) that can verify that something did NOT happen, except the NOT emission of an event. |
this is definitely useful and we should add this imo. otherwise, you can't test the inverse of expectEmit. flipping the impl and adding will try to squeeze this in |
+1 |
def a +1 from my side for this change, too. Hardhat has a similar functionality that I found really easy and handy to use: expect(<call).to.not.emit(, ).withArgs(....) the ".withArgs" part is optional. So either I check only if an event was emitted or not or I can also check if the args match (or dont). |
+1 |
+1 Another way to go about this would be with cheatcodes that count the number of times a function is called or event emitted. That way we can not only assert that call/emit was made multiple times, but also if it was not made at all using the same cheat. There is an issue for |
I'm supportive of adding overloads with a Also if anyone has objections to that syntax and has a reason to prefer e.g. |
Yep, you can assign it to me @mds1 |
woops this one slipped through... sorry about that nice @reubenr0d ! lmk if you have questions/need some pointers |
cc @mds1 i guess we went for the |
Yea the count approach is how we'll implement negative assertions. We have it for |
expectEmit
and expectRevert
expectEmit
and expectRevert
expectEmit
and expectRevert
using count
syntax
cc @grandizzy possibly relevant to recent cheatcode additions |
yep, is on my cheatcode expect* list to tackle |
Negative assertions, e.g. asserting that a certain
call
was not performed or a certain event was notemit
ed are not very useful on their own (because they might pass for the wrong reasons, e.g. a negative assertion always passes if you simply don't do anything of course).However, when paired with positive assertions (e.g. asserting that the tested functionality actually performed the desired state mutation), they can be very useful to assert that a certain code path was skipped in doing so. This can be useful to test e.g. that a certain conditional was skipped.
I realize that this can already be achieved at the test level with the
testFail
prefix, but that comes with different ergonomics vs. negating individual assertions.The text was updated successfully, but these errors were encountered: