How can I use parameterFilter parameter with switch parameter when taunted in Pester?

Using Pester, I mock an advanced feature that includes, among other parameters, a switch. How to create -parameterFilterfor a layout that includes a switch option?

I tried:

-parameterFilter { $Domain -eq 'MyDomain' -and $Verbose -eq $true }

-parameterFilter { $Domain -eq 'MyDomain' -and $Verbose }

-parameterFilter { $Domain -eq 'MyDomain' -and $Verbose -eq 'True' }

to no avail.

+4
source share
3 answers

Try the following:

-parameterFilter { $Domain -eq 'MyDomain' -and $Verbose.IsPresent}
+4
source

-Verbose , . $Verbose , . , - -Verbose, , $VerbosePreference Continue SilentlyContinue.

Verbose $PSBoundParameters, :

Mock someFunction -parameterFilter { $Domain -eq 'MyDomain' -and $PSBoundParameters['Verbose'] -eq $true }
+1

, :

Test.ps1. . , Test-Call Mocked-Call. Mocked-Call .

Function Test-Call {
    param(
        $text,
        [switch]$switch
    )

    Mocked-Call $text -switch:$switch
}

Function Mocked-Call {
    param(
        $text,
        [switch]$switch
    )

    $text
}

Test.Tests.ps1 . This is our actual test script. Please note that for Mocked-Callthere are two breadboard implementations. The first will match if the parameter is switchset to true. The second will correspond when the parameter texthas the value of the fourth and , the parameter switchhas the value false.

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "Test-Call" {

    It "mocks switch parms" {
        Mock Mocked-Call { "mocked" } -parameterFilter { $switch -eq $true }
        Mock Mocked-Call { "mocked again" } -parameterFilter { $text -eq "fourth" -and $switch -eq $false }

        $first = Test-Call "first" 
        $first | Should Be "first"

        $second = Test-Call "second" -switch
        $second | Should Be "mocked"

        $third = Test-Call "third" -switch:$true
        $third | Should Be "mocked"

        $fourth = Test-Call "fourth" -switch:$false
        $fourth | Should Be "mocked again"

    }
}

Running tests shows green:

Describing Test-Call
[+]   mocks switch parms 17ms
Tests completed in 17ms
Passed: 1 Failed: 0
0
source

All Articles