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

Add relaxed versions of Assert.ThrowsException and Assert.ThrowsExceptionAsync #4257

Open
Evangelink opened this issue Dec 6, 2024 · 2 comments

Comments

@Evangelink
Copy link
Member

Summary

Allows to assert some derived exceptions are thrown.

Background and Motivation

Assert.ThrowsException<T> and Assert.ThrowsExceptionAsync<T> are checking that the exception throws is exactly matching the one specified as generic type argument. It can often happen that you would instead prefer to say type or derived types.

Proposed Feature

Add Assert.ThrowsAnyException<T>/Assert.ThrowsAnyExceptionAsync<T> or add overloads of the current APIs with a boolean allowing to control whether to check exact type or derived.

Alternative Designs

Relax current APIs and introduce Assert.ThrowsExactException<T> and Assert.ThrowsExactExceptionAsync. Although I prefer the naming, this would introduce a potentially strong functional breaking change for users so I would be reluctant to do it.

@nohwnd
Copy link
Member

nohwnd commented Dec 9, 2024

My preferred syntax would also be changing the current to "any" and ThrowsExactException. But I see why that is not possible.

So with that not being an option I think the proposed design is the best. I do like the name being reasonably simple.

@Evangelink
Copy link
Member Author

The simplest idea would be to add ThrowsAnyException(Async) APIs but the name could be seen as confusing.

Given that in v4 we would like to ensure simplified naming, and for example remove the Exception part of the API, it seems better to:

  1. Add 2 new APIs (Throws(Async) and ThrowsExactly(Async))
  2. Add an analyzer + code fix to change ThrowsException(Async) into ThrowsExactly(Async)
  3. In v4 either obsolete the older API or obsolete in a later release and drop in v4

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

No branches or pull requests

2 participants