Search code examples
powershellpester

How to test the resolved parameter set?


I'm trying to write a basic Pester test that checks an "advanced" function for the resolved parameter set:

function Do-Stuff
{
    [CmdletBinding(DefaultParameterSetName='Set 1')]
    [OutputType([String])]

    Param
    (
        [Parameter(ParameterSetName='Set 1')] 
        [switch]
        $S1,

        [Parameter(ParameterSetName='Set 2')]
        [switch]
        $S2
    )

    $PSBoundParameters |select -ExpandProperty Keys
}

Describe Do-Stuff {
    It 'Returns "S2" when switch "S2" is set' {
        $actual = Do-Stuff -S2 
        $expexted = 'S2'
        $actual |Should Be $expexted
    }

    # How to test the resolved parameter set?
    It 'The resolved parameter set is "Set 2" when switch "S2" is set' { 
        $actual = 'What to do here?' # I'm lost ;(
        $expexted = 'Set 2'
        $actual |Should Be $expexted
    }
}

Thanx. Any advice would be highly appreciated since I'm totally new to Pester. ...not much better in posh and coding in general either :D


Solution

  • The following will test if you use 'Set 2' for parameter S2:

    Describe Do-Stuff {
        $Command = Get-Command 'Do-Stuff'
    
        It 'Returns "S2" when switch "S2" is set' {
            $actual = Do-Stuff -S2 
            $expexted = 'S2'
            $actual |Should Be $expexted
        }
    
    # How to test the resolved parameter set?
        It 'The resolved parameter set is "Set 2" when switch "S2" is set' { 
            $actual = $Command.Parameters["S2"].ParameterSets.Keys
            $expexted = 'Set 2'
            $actual |Should Be $expexted
            # when you use several sets for parameters
            $expexted -contains $actual | should Be $true
       }
    

    }

    Tracing if powershell is actually executing 'set 2', when you set it so, not a subject for pester testing imho...