【发布时间】:2023-03-28 02:47:01
【问题描述】:
我试图弄清楚如何让 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
}
}
Find-Waldo.ps1
Param (
[Parameter(Mandatory)]
[String]$ComputerName,
[String]$ScriptName
)
Function Find-Waldo {
[CmdletBinding()]
Param (
[String]$FilePath
)
'Do something'
}
每次我尝试assert 结果或只是运行测试时,它都会提示我输入ComputerName 参数,而不是使测试失败。
我在这里遗漏了一些非常明显的东西吗?有没有办法测试强制参数的存在?
【问题讨论】:
-
您不应该尝试以这种方式测试
Mandatory属性,as per this comment from the team -
你能举个例子说明如何在上面的例子中使用
((Get-Command Get-Command).Parameters['Name'].Attributes | ? { $_ -is [parameter] }).Mandatory | Should Be $false吗? -
Get-Command也适用于脚本文件:(Get-Command "$here\$sut").Parameters -
谢谢 Mathias,我已经这样解决了
(Get-Command "$here\$sut").Parameters['ComputerName'].Attributes.Mandatory | Should be $true如果您发布此问题,我会将其标记为已解决。
标签: powershell pester