相关疑难解决方法(0)

我不明白使用begin/process/end块对函数进行param绑定

function Format-File {
  param(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string] $path=$(throw "path is mandatory ($($MyInvocation.MyCommand))"),

    [Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)]
    [ValidateNotNullOrEmpty()]
    [string] $key,

    [Parameter(Mandatory = $true, Position = 2, ValueFromPipelineByPropertyName = $true)]
    [ValidateNotNullOrEmpty()]
    [string] $value
  )
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼它,假设我已经为字典添加了值(为了简洁而删除了)

$dict = New-Object 'System.Collections.Generic.Dictionary[string,string]'
$dict.GetEnumerator() | Format-File -Path $updatePath
Run Code Online (Sandbox Code Playgroud)

这是我的难题.

以上工作完美.但是,以下没有,请注意键/值参数的不同之处

function Format-File {
  param(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string] $path=$(throw "path is mandatory ($($MyInvocation.MyCommand))"),

    [Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = …
Run Code Online (Sandbox Code Playgroud)

powershell

5
推荐指数
1
解决办法
742
查看次数

使用Pester测试强制性参数

我试图弄清楚如何对丢失的参数进行Pester测试:

Find-Waldo.Tests.ps1

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

Describe 'Mandatory paramters' {
    it  'ComputerName' {
        {
            $Params = @{
                #ComputerName = 'MyPc'
                ScriptName   = 'Test'
            }
            . "$here\$sut" @Params
        } | Should throw
    }
}
Run Code Online (Sandbox Code Playgroud)

Find-Waldo.ps1

Param (
    [Parameter(Mandatory)]
    [String]$ComputerName,
    [String]$ScriptName
)

Function Find-Waldo {
    [CmdletBinding()]
    Param (
        [String]$FilePath
    )

    'Do something'
}
Run Code Online (Sandbox Code Playgroud)

每次我尝试assert结果或仅运行测试时,都会提示我输入ComputerName参数,而不是使测试失败。

我在这里想念什么超级明显吗?有没有办法测试强制性参数的存在?

powershell pester

5
推荐指数
1
解决办法
936
查看次数

标签 统计

powershell ×2

pester ×1