Assert-Error

Asserts that there are errors, and/or that a specific error's messages matches a regular expression.

Syntax

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>]

Description

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.

Parameters

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

EXAMPLE 1

Assert-Error

Demonstrates how to check that there is at least one error.

EXAMPLE 2

Assert-Error 0 'not found'

Demonstrates how to check that the last error (remember, $Error is a stack) matches the regular expression not found.

EXAMPLE 3

Assert-Error -1 'not found'

Demonstrates how to check that the first error (remember, $Error is a stack) matches the regular expression not found.

EXAMPLE 4

Assert-Error -Last 'not found'

Demonstrates how to check that the last error matches the regular exprssion not found without worrying about indexes.

EXAMPLE 5

Assert-Error -First 'not found'

Demonstrates how to check that the first error matches the regular exprssion not found without worrying about indexes.