如何将两个数组与 Pester 中的自定义对象进行比较



我们正在努力让 Pester 测试失败或通过,具体取决于arrayobjects的相等性。

测试.ps1

#require Assert
#require Pester
$Expected = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)
$Actual = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)
Describe 'comparing arrays' {
Context 'Assert-Equivalent' {
it 'should be green' {           
Assert-Equivalent -Actual $Expected -Expected $Expected
}
it 'should be green' {        
Assert-Equivalent -Actual $Actual -Expected $Expected
}
it 'should be red' {        
$Wrong = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'WROMG';Name2 = 'Text2'}
)
Assert-Equivalent -Actual $Wrong -Expected $Expected
}
}
Context 'Should be' {
it 'should be green' {
$Expected | Should -Be $Expected
}
it 'should be green' {
$Actual | Should -Be $Expected
}
it 'should be red' {        
$Wrong = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'WROMG';Name2 = 'Text2'}
)
$Wrong | Should -Be $Expected
}
}
}

我们似乎做不好。我们是否使用了错误的 CmdLet?还是有另一种检查方法?有时array也只是另一个对象的属性。因此,需要进行深入比较。

当我想将一些"复杂"对象与 Pester 进行比较时,我使用ConvertTo-Json。这并不理想,因为如果类型不匹配,可能会有一些漏报,但它通常可以完成这项工作。

$Expected = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)
$Actual = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)
Describe 'comparing arrays' {
Context 'Assert-Equivalent' {
it 'should be green' {           
Assert-Equivalent -Actual ($Expected | ConvertTo-Json)  -Expected ($Expected | ConvertTo-Json) 
}
it 'should be green' {        
Assert-Equivalent -Actual ($Actual | ConvertTo-Json)  -Expected ($Expected | ConvertTo-Json) 
}
it 'should be red' {        
$Wrong = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'WROMG';Name2 = 'Text2'}
)
Assert-Equivalent -Actual ($Wrong | ConvertTo-Json)  -Expected ($Expected | ConvertTo-Json) 
}
}
Context 'Should be' {
it 'should be green' {
($Expected | ConvertTo-Json) | Should -Be ($Expected | ConvertTo-Json)
}
it 'should be green' {
($Actual | ConvertTo-Json) | Should -Be ($Expected | ConvertTo-Json)
}
it 'should be red' {        
$Wrong = @(
[PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
[PSCustomObject]@{Name1 = 'WROMG';Name2 = 'Text2'}
)
($Wrong | ConvertTo-Json) | Should -Be ($Expected | ConvertTo-Json)
}
}
}

这取决于你想在这个问题上有多严格,例如:

  • 数组中对象的顺序重要吗?
  • 是否要对属性进行类型转换?

无论如何,我为此写了一个小助手:

Function Should-BeObject {
Param (
[Parameter(Position=0)][Object[]]$b, [Parameter(ValueFromPipeLine = $True)][Object[]]$a
)
$Property = ($a | Select-Object -First 1).PSObject.Properties | Select-Object -Expand Name
$Difference = Compare-Object $b $a -Property $Property
Try {"$($Difference | Select-Object -First 1)" | Should -BeNull} Catch {$PSCmdlet.WriteError($_)}
}

您可以像这样调用它:

,$Actual | Should-BeObject $Expected

(注意前面的逗号,$Actual(

我自己没有尝试过,但是 https://github.com/nohwnd/Assert 库中有一个看起来很有前途的Assert-Equivalentcmdlet。

虽然这是一个古老的问题,但我想展示另一种方法。在比较复杂对象时,使用比较对象可能会派上用场。


$Expected = @(
[PSCustomObject]@{Name1 = 'Text1'; Name2 = 'Text2' }
[PSCustomObject]@{Name1 = 'Text1'; Name2 = 'Text2' }
)
$Actual = @(
[PSCustomObject]@{Name1 = 'Text1'; Name2 = 'Text2' }
[PSCustomObject]@{Name1 = 'Text1'; Name2 = 'Text2' }
)
Describe 'object equivalence' {
It 'eq' {
$eqs = $Actual | Compare-Object -ReferenceObject $Expected -Property @('Name1', 'Name2') -IncludeEqual | Select-Object -ExpandProperty SideIndicator
$eqs | ForEach-Object { $_ | Should -Be '==' }
}
}

请注意,您需要指定要比较的所有属性。在这里查看更多内容。

相关内容

  • 没有找到相关文章

最新更新