简体   繁体   English

如何测试解析的参数集?

[英]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: 我正在尝试编写一个基本的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
    }
}

Thanx. 谢谢 Any advice would be highly appreciated since I'm totally new to Pester. 因为我是Pester的新手,所以任何建议都将不胜感激。 ...not much better in posh and coding in general either :D ...一般而言,在豪华和编码方面也没有什么好:D

You would use the Trace-Command cmdlet for this. 您将为此使用Trace-Command cmdlet

The -Name parameter would be set to ParameterBinderController . -Name参数将设置为ParameterBinderController

As a way to get started, try something like this (outside of pester) 作为一种入门方法,请尝试这样的操作(在pester之外)

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

The -PSHost options sends the output to the host so you can see it. -PSHost选项将输出发送到主机,以便您可以看到它。

You will probably want to not use that in your Pester test, and experiment with the other listener options and methods of catching the output. 您可能不希望在Pester测试中使用它,而是尝试使用其他侦听器选项和捕获输出的方法。

The following will test if you use 'Set 2' for parameter S2: 下面将测试如果使用“设置2”的参数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... 跟踪powershell是否实际上在执行“ set 2”(设置为2),而不是进行虫害测试的主题...

Use as follows: 用法如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM