Closed aolszowka closed 1 year ago
Thanks for the report and sorry for the late response.
There won't be any change to this regarding Pester v4. I have just submitted a PR for Pester v5 to improve the error when using Should -Throw
without providing a scriptblock as input as it was currently not working as intended. See #2354 for before and after examples.
Your initial sample doesn't use Should -Throw
at all, so I don't think we can improve that. Exceptions in It
-blocks are also used by custom assertions (not Should
) so would be hard to distinguish mistakes from intentional exceptions to provide a hint in Diagnostic-logs.
Location
https://pester.dev/docs/v4/usage/assertions#throw
Should -Throw
General summary of the issue
The documentation is actually very good for
Should -Throw
however being so new to Pester this threw me for a loop:This returns an error message similar to the following:
The problem is a misunderstanding on my part that you MUST put any throwing operation in braces:
{ }
this is actually called out in this StackOverflow post from @markwragg https://stackoverflow.com/a/45574334/433069The following works:
I am not sure how best the documentation could be improved, I think perhaps having the error message that occurs if you do it "wrong" might be helpful. In some ways the documentation tries to call this out with this blurb:
Warning: The input object must be a ScriptBlock, otherwise it is processed outside of the assertion.
The hope is that someone else encountering this will run across this issue eventually.