我正在尝试弄清楚如何让 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
参数而不是测试失败。我是否忽略了什么非常明显的东西?有没有一种方法可以测试必需参数的存在?
((Get-Command Get-Command).Parameters['Name'].Attributes | ? { $_ -is [parameter] }).Mandatory | Should Be $false
,就像上面的例子一样? - DarkLite1Get-Command
works on script files as well:(Get-Command "$here\$sut").Parameters
- Mathias R. Jessen(Get-Command "$here\$sut").Parameters['ComputerName'].Attributes.Mandatory | Should be $true
如果您发布了这个,我会标记为已解决。 - DarkLite1