Skip to content

Add‐AssertionOperator

Jeff Crosby edited this page Jun 29, 2019 · 3 revisions

Description

This function allows you to create custom Should assertions.

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 not 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
Clone this wiki locally