-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Where should I propose Assertions for Nested Exceptions? Here or Pester? #25
Comments
Assert-Throw is only meant to encapsulate the use cases that everyone would likely find useful. Stuff like message, exceptiontype, fqeid. For anything else it outputs the exception object and you can drill in it in with other assertions.
I also wrote a piece of code that enables combining the assertions in one failure somewhere. This I would like to make part of the framework, so multiple assertions in a test would not be a problem.
We could provide another assertion that asserts on exception objects to make this drilling down easier, the difficult part is naming it. Assert-Throw, Assert-Error and Assert-Exception are all the same thing in layman’s eyes so I don’t want to overload them.
Could you think about it a bit and propose some syntax so we can brainstorm over it further? I am looking for nice non-complex syntax and features that are useful to many people.
I can for example imagine that something like -Level 1 that would assert on first InnerException would be useful in some cases, but how often would you need to go deeper, and how easy is to guess what the parameter does? So in that case it might be better to call it -InnerException, even though we lose the ability to go deeper (without introducing parameter scopes).
|
Oh I see. Would you support this sort of usage? { ThisThrowsANestedException } |
Assert-Throw |
Assert-Message 'some deeply nested error message' -AnyWhereIn 'Inner','Aggregate' I'm not sure where |
This is the kind of custom DSL, non-powershelly stuff that I would like to avoid, unless we have a really good solution that makes powershell-sense (dibz on that term). We probably need more realistic example for this. We should also keep in mind that we are testing one specific thing, so the syntax afaik only about navigation and encapsulating useful cases. but please correct me if I am wrong. Last time I saw aggregate exception was from EntityFramework, and threre I always have a piece of code that flattens the exception. This is a bit of extreme scenario, where you get $exception = { ThisThrowsExceptionWithAggregatedExceptionInTheInnerException } | Assert-Throw
$exception.InnerException.InnerExceptions |
Assert-Any -FilterScript {
$_.Message -like '*message on some of the aggregate exceptions*' } I also think there will need to be a set of cmdlets for $exception = { ThisThrowsExceptionWithAggregatedExceptionInTheInnerException } | Assert-Throw
$exception.InnerException.InnerExceptions |
Assert-Any -FilterScript {
Test-Exception -ExceptionMessage '*message on some of the aggregate exceptions*' `
-Type ([ArgumentException]) } As I said when I was responding from phone: I can imagine a simpler scenario where someone is wrapping assertions for you and you are tired of unwrapping them (like { ... } | Assert-Throw -Type ([ArgumentException]) -InnerException
# or
{ ... } | Assert-Throw -Type ([ArgumentException]) -Level 1 #or -Unwrap 1 But I also see this being useful only rarely. |
And the composition is in #19 :) |
What aspect do you regard as "non-powershelly"?
I encounter both
One reason I'm interested in this capability is because I regularly need to test that an exception thrown was about one particular domain-specific thing, not some other domain-specific thing, or some unrelated machinery thing like a null reference exception. I'd say this is occurs more frequently for integration tests but I don't see why it wouldn't occur when testing a PowerShell unit that includes some non-trivial but side-effect free .Net object. In concrete terms, the need for better exception matching most recently arose because I needed to assert which reason |
In C# that rarely happens unless you call For the case when we don't care about the origin of the exception I would sugguest a function much like the one you have in your code, but with whole exception objects, that would flatten the Exception tree into a collection, and then apply Assert-Any on it. Maybe we could call it It's too late I will need to review this again tomorrow. |
I agree. I was more focused on whether piping from one
I hadn't considered this until now. I think I have some homework to here first. |
I don't have a compelling use case for assertions on AggregateException, so I'd like to drop that from discussion and resume this discussion for assertions for nested exceptions that arise when the I've experimented with some approaches to such an assertion. The best approach I've found so far is in #28 for discussion. |
Neither the "-Throw" assertion in the Pester project nor
Assert-Throw
currently support search of nested or aggregate exceptions. I expect to implement such an assertion. Where should I propose this addition?The text was updated successfully, but these errors were encountered: