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

谢谢。任何建议都将受到高度赞赏,因为我对 Pester 完全陌生。...在豪华和编码方面也没有好多少:D

4

3 回答 3

2

您将为此使用Trace-Commandcmdlet

-Name参数将设置为ParameterBinderController

作为开始的一种方式,尝试这样的事情(除了纠缠之外)

Trace-Command -Name ParameterBinderController -Expression { Do-Stuff -S2 } -PSHost

这些-PSHost选项将输出发送到主机,以便您查看。

您可能不想在 Pester 测试中使用它,并尝试使用其他侦听器选项和捕获输出的方法。

于 2015-12-19T17:16:08.553 回答
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 回答
0

使用如下:

Function Main{
  [CmdletBinding(SupportsShouldProcess=$true,DefaultParameterSetName="ViewOnly")]
Param(

[Parameter(ParameterSetName="ViewOnly")]
   [switch]$ViewOnly,

[Parameter(ParameterSetName="NukeAll")]
 [switch]$NukeAll
)

Switch ($PSCmdlet.ParameterSetName){
 "NukeAll"{
NukeAll
 }#end nuke all
"ViewOnly"{
ViewOnly
}#end viewonly
  }#end Switch

Function NukeAll {
  #Do NukeAll function code here.
 }

Function ViewOnly{
  #Do ViewOnly function code here.
 }
于 2015-12-19T17:15:55.540 回答