Asserts that there are errors, and/or that a specific error's messages matches a regular expression.
Assert-Error [-Count <Int32>] [[-Message] <String>] [<CommonParameters>]
Assert-Error -Last [-Count <Int32>] [-Regex] <Regex> [[-Message] <String>] [<CommonParameters>]
Assert-Error -First [-Count <Int32>] [-Regex] <Regex> [[-Message] <String>] [<CommonParameters>]
Assert-Error [-Index] <Int32> [-Count <Int32>] [-Regex] <Regex> [[-Message] <String>] [<CommonParameters>]
With no parameters, checks that there are errors in the $Error
array.
If passed an index and a regular expression, checks that there are errors in the $Error
array, that there is one at index Index
, and that it's message matches $Regex
.
If you want to check the last/most recent error, use the -Last
switch. To check the first/oldest error, use the -First
switch.
Name | Type | Description | Required? | Pipeline Input | Default Value |
---|---|---|---|---|---|
Last | SwitchParameter | Checks the last/most recent error. |
true | false | False |
First | SwitchParameter | Checks the first/oldest error. |
true | false | False |
Index | Int32 | The index of the error to check. |
true | false | 0 |
Count | Int32 | Check the number of errors. |
false | false | 0 |
Regex | Regex | The regular expression to check. |
true | false | |
Message | String | A message to show if the assertion fails. |
false | false |
Assert-Error
Demonstrates how to check that there is at least one error.
Assert-Error 0 'not found'
Demonstrates how to check that the last error (remember, $Error
is a stack) matches the regular expression not found
.
Assert-Error -1 'not found'
Demonstrates how to check that the first error (remember, $Error
is a stack) matches the regular expression not found
.
Assert-Error -Last 'not found'
Demonstrates how to check that the last error matches the regular exprssion not found
without worrying about indexes.
Assert-Error -First 'not found'
Demonstrates how to check that the first error matches the regular exprssion not found
without worrying about indexes.