Skip to content

Add‐AssertionOperator

Andrew Sears edited this page Dec 13, 2019 · 3 revisions

This function allows you to create custom Should assertions.

Example

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