Skip to main content
Version: v4

Add-AssertionOperator

SYNOPSIS#

Register an Assertion Operator with Pester

SYNTAX#

Add-AssertionOperator [-Name] <String> [-Test] <ScriptBlock> [[-Alias] <String[]>] [[-InternalName] <String>] [-SupportsArrayInput] [<CommonParameters>]

DESCRIPTION#

This function allows you to create custom Should assertions.

EXAMPLES#

EXAMPLE 1#

function BeAwesome($ActualValue, [switch] $Negate){
    [bool] $succeeded = $ActualValue -eq 'Awesome'    if ($Negate) { $succeeded = -not $succeeded }
    if (-not $succeeded)    {        if ($Negate)        {            $failureMessage = "{$ActualValue} is Awesome"        }        else        {            $failureMessage = "{$ActualValue} is not Awesome"        }    }
    return New-Object psobject -Property @{        Succeeded      = $succeeded        FailureMessage = $failureMessage    }}
Add-AssertionOperator -Name  BeAwesome `                    -Test  $function:BeAwesome `                    -Alias 'BA'
PS C:\> "bad" | should -BeAwesome{bad} is not Awesome

PARAMETERS#

-Name#

The name of the assertion. This will become a Named Parameter of Should.

Type: StringParameter Sets: (All)Aliases:
Required: TruePosition: 1Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False

-Test#

The test function. The function must return a PSObject with a [Bool]succeeded and a [string]failureMessage property.

Type: ScriptBlockParameter Sets: (All)Aliases:
Required: TruePosition: 2Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False

-Alias#

A list of aliases for the Named Parameter.

Type: String[]Parameter Sets: (All)Aliases:
Required: FalsePosition: 3Default value: @()Accept pipeline input: FalseAccept wildcard characters: False

-InternalName#

If -Name is different from the actual function name, record the actual function name here. Used by Get-ShouldOperator to pull function help.

Type: StringParameter Sets: (All)Aliases:
Required: FalsePosition: 4Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False

-SupportsArrayInput#

Does the test function support the passing an array of values to test.

Type: SwitchParameterParameter Sets: (All)Aliases:
Required: FalsePosition: NamedDefault value: FalseAccept pipeline input: FalseAccept wildcard characters: False

CommonParameters#

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS#

OUTPUTS#

NOTES#

RELATED LINKS#

EDIT THIS PAGE#

This page was auto-generated using Pester's comment based help. To edit the content of this page, change the corresponding help in the pester/Pester v4 repository. See our contribution guide for more information.