pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.11k stars 473 forks source link

Support multiple exception types with `Should -Throw` #1525

Closed vexx32 closed 5 months ago

vexx32 commented 4 years ago

1. General summary of the issue

Currently, Should -Throw -ExceptionType only supports one type of exception being provided. In most cases, this works well, but for modules that need to be cross-platform and work with older versions of PS or .NET as well as recent versions, some exception types expected may differ depending on the version of .NET the module ends up running on.

2. Describe Your Environment

N/A

3. Expected Behavior

Should -Throw should support multiple exception types and/or messages to be provided to simplify tests that need to work on more than one version of PS / .NET.

4.Current Behavior

Currently, Should -Throw only supports a single type name to be passed to -ExceptionType (and -ExceptionMessage).

5. Possible Solution

One or both of these could be possible:

$script | Should -Throw -ExceptionType $type1, $type2
$script | Should -Throw -Exception @(
    @{ Type = $type1; Message = $message1 }
    @{ Type = $type2; Message = $message2 }
)
nohwnd commented 5 months ago

Closing as not planned, this is easy to check by outputting the error from Should -Throw and doing any assertion you'd like on it.

$err = { throw [System.InvalidOperationException]::new() } | Should -Throw -PassThru
$err.Exception.GetType() | Should -BeIn @([System.InvalidCastException], [System.ArgumentException])
Expected collection @([System.InvalidCastException], [System.ArgumentException]) 
to contain [System.InvalidOperationException], but it was not found.
At S:\p\pester\src\functions\assertions\Should.ps1:252 char:13
+             throw $errorRecord
+             ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidResult: (System.Collections.…ring,System.Object]:Dictionary`2) [], Exception
+ FullyQualifiedErrorId : PesterAssertionFailed