This weekend I added type filter and -PassThru
to Should -Throw
. Let’s see how are they useful.
🔥 This feature is not released yet, get pre-release version 4.2.0-alpha3 to use it.
Type filter
Filtering exceptions on type is one of the most basic capabilities of any assertion that deals with exceptions, yet we were missing it till now. But there it is now:
Describe "Get-Computer" { |
The type filter matches the exception type and any of it’s subtypes. This means that throwing ArgumentException
or ArgumentNullException
would make the previous test pass. This might be surprising to you, but catching the least specific exception from the same family makes your tests less brittle, and hence easier to change.
Better error messages
The error messages got better, because now you see which filters were not matched, instead of all the filters that you applied, like this:
Describe "Get-Computer" { |
Should -Not -Throw changed behavior
Should -Not -Throw
the ugly sister of Should -Throw
, was swallowing errors when a filter was defined and the thrown exception was not met. This did not make sense, the premise of Should -Not -Throw
is that an exception was not thrown. So when any exception is thrown the assertion should fail, no matter what filter you specify. The recommended practice is simply not using it, because every line is an implicit should not throw.
PassThru
There is a new parameter for that allows you to pass the caught error object so you can examine it further. This is useful when you are interested in the inner exception, or when you catch an aggregate exception and need to extract some info from it.
$sb = { throw (New-Object Exception -ArgumentList "ex1", |
Here we are interested in the message of the inner-inner exception. So we capture the object, and then use Should -Be
to further examine it.
Summary
Should -Throw
got better, so try to use it and help the project by reporting how it works on twitter, powershell slack or our repo. Also checkout other cool features, like -Because
or -HaveCount
. For full listing see the our changelog.