1

解決されたパラメーター セットの「高度な」関数をチェックする基本的な Pester テストを作成しようとしています。

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
    }
}

ありがとう。私はペスターにまったく慣れていないので、アドバイスをいただければ幸いです。...一般的には、上品でコーディングもそれほど良くありません:D

4

3 に答える 3

2

以下は、パラメータ S2 に「Set 2」を使用するかどうをテストします。

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
   }

}

powershellが実際に「set 2」を実行しているかどうかを追跡します。設定すると、ペスターテストの対象ではありません...

于 2015-12-20T09:54:01.660 に答える