Feature Request: Add support for recursive checking of inner exception
See original GitHub issueDescription
Sometimes, when writing high-level tests, we expect a function to throw an exception with a deeply nested inner exception. Writing assertions can be tedious if we only care about the existence of a certain type in the chain and not how deep it is located.
So in short, I would like to write something like this:
action.Should().Throw<ExceptionA>()
.WithInnerException<ExceptionB>(recursive: true);
instead of:
action.Should().Throw<ExceptionA>()
.Which.InnerException.InnerException.InnerException.BeOfType<ExceptionB>();
Issue Analytics
- State:
- Created 2 years ago
- Comments:33 (25 by maintainers)
Top Results From Across the Web
Fetching the Last inner Message from C# exception Object ...
I have modified your code you do not need to set the message to empty while you checking ex.InnerException == null just return...
Read more >Using recursive queries - Db2 for i SQL
In a connect by query you can use any type of join supported by Db2 for i including INNER JOIN, LEFT OUTER JOIN,...
Read more >Recursive methods using C# - CodeProject
InnerExceptions. Recursive methods are useful for getting the last innerException : C#. public Exception GetInnerException(Exception ex) ...
Read more >StackOverflowException Class (System)
The exception that is thrown when the execution stack exceeds the stack size. This class cannot be inherited.
Read more >AWS Lambda function errors in C# - ...
This page describes how to view Lambda function invocation errors for the C# runtime using the Lambda console and the AWS CLI.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think that’s a great idea and I would prefer
WhoseInnerExceptionChain
to emphasize that that it exposes not just the direct inner exceptions.Another possibility for this would be to convert it into a normal collection assertion with something like:
This would be similar to the
WhoseValue
used in places like dictionaries and leverage all the collection assertions that already exist on top of the flattened list of exceptions (that could also be empty). It could also be a computedIEnumerable
(i.e. deferred execution), depending on how we want to go about it.I think the “Chain” wording at the end makes sense, but perhaps there is a better way to put it (perhaps just
WhoseInnerExceptions
, even though that could be a bit misleading when dealing with anAggregateException
).From what I’m seeing in the docs, it seems we don’t have a generic
Contain<T>
assertion, so I’d probably add that as well to make this work, since it would be a valid assertion to have for normal collections too (would be useful for polymorphic collections). Naming could either beContain<T>
, or maybe a more explicitContainItemOfType<T>
to go along the other “Contain…Items…” methods. While we are at it, a “Single” version should also be added to match.