Pester Assert-MockCalled 的 ParameterFilter 中的断言数组
Assert array in ParameterFilter for Pester Assert-MockCalled
我们正在尝试断言一个带有特定参数的调用,该参数是一个数组,它在 Assert-MockCalled 中返回 false,对于所有其他不是数组的参数正在工作(字符串类型)。
这是我的例子:
function NewZip ($Name) {
$compress = @{
Path = "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico"
CompressionLevel = "Fastest"
DestinationPath = "$PSScriptRoot/$Name.zip"
}
Compress-Archive @compress -Update
}
It "Creates a zip file" {
# Arrange
$name = "test"
$assertParams = @{
CommandName = 'Compress-Archive'
Times = 1
ParameterFilter = {
$Path -in "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico" -and
$DestinationPath -eq "$PSScriptRoot/$name.zip" -and
$CompressionLevel -eq "Fastest" -and
$Update -eq $true
}
}
Mock Compress-Archive {}
# Act
NewPackage -Name $name
# Assert
Assert-MockCalled @assertParams
}
如何在 Assert-MockCalled 中使用数组比较?
$Path -in "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico"
无法工作because:
When the test object is a set, these operators use reference equality to check whether one of the set's elements is the same instance of the test object.
您可以使用 Compare-Object
来进行值比较:
ParameterFilter = {
-not (Compare-Object $Path "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico") -and
$DestinationPath -eq "$PSScriptRoot/$name.zip" -and
$CompressionLevel -eq "Fastest" -and
$Update -eq $true
}
需要求反,因为 Compare-Object
输出差异。如果数组相等,则不输出任何内容。捕获时,“无”变成 $null
,在布尔上下文中转换为 $false
。
为了在表达式中使用 Compare-Object
的输出,需要括号(又名 group operator)。
这是一个棘手的问题!我花了大部分时间在这上面。
这是失败的:
Mock Invoke-MyFunc { } -Verifiable -ParameterFilter { $realArray -eq $testArray }
因为上面的答案很详细,所以你不能用这种方式比较数组。
我最终得到:
Mock Invoke-MyFunc { } -Verifiable -ParameterFilter { (Compare-Object $realArray $testArray).length -eq 0 }
因为如果长度为零,数组是相等的,我们需要在ParameterFilter中return一个boolean
。
我们正在尝试断言一个带有特定参数的调用,该参数是一个数组,它在 Assert-MockCalled 中返回 false,对于所有其他不是数组的参数正在工作(字符串类型)。
这是我的例子:
function NewZip ($Name) {
$compress = @{
Path = "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico"
CompressionLevel = "Fastest"
DestinationPath = "$PSScriptRoot/$Name.zip"
}
Compress-Archive @compress -Update
}
It "Creates a zip file" {
# Arrange
$name = "test"
$assertParams = @{
CommandName = 'Compress-Archive'
Times = 1
ParameterFilter = {
$Path -in "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico" -and
$DestinationPath -eq "$PSScriptRoot/$name.zip" -and
$CompressionLevel -eq "Fastest" -and
$Update -eq $true
}
}
Mock Compress-Archive {}
# Act
NewPackage -Name $name
# Assert
Assert-MockCalled @assertParams
}
如何在 Assert-MockCalled 中使用数组比较?
$Path -in "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico"
无法工作because:
When the test object is a set, these operators use reference equality to check whether one of the set's elements is the same instance of the test object.
您可以使用 Compare-Object
来进行值比较:
ParameterFilter = {
-not (Compare-Object $Path "$PSScriptRoot/terms.txt", "$PSScriptRoot/fav.ico") -and
$DestinationPath -eq "$PSScriptRoot/$name.zip" -and
$CompressionLevel -eq "Fastest" -and
$Update -eq $true
}
需要求反,因为 Compare-Object
输出差异。如果数组相等,则不输出任何内容。捕获时,“无”变成 $null
,在布尔上下文中转换为 $false
。
为了在表达式中使用 Compare-Object
的输出,需要括号(又名 group operator)。
这是一个棘手的问题!我花了大部分时间在这上面。
这是失败的:
Mock Invoke-MyFunc { } -Verifiable -ParameterFilter { $realArray -eq $testArray }
因为上面的答案很详细,所以你不能用这种方式比较数组。
我最终得到:
Mock Invoke-MyFunc { } -Verifiable -ParameterFilter { (Compare-Object $realArray $testArray).length -eq 0 }
因为如果长度为零,数组是相等的,我们需要在ParameterFilter中return一个boolean
。