2

objects我们正在努力让 Pester 测试失败或通过,具体取决于array.

测试.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
        }
    }
}

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

4

4 回答 4

4

当我想将一些“复杂”对象与 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)
        }
    }
}
于 2018-06-15T08:55:49.670 回答
2

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

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

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

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

于 2018-06-15T09:52:51.170 回答
0

我自己没有尝试过,但是https://github.com/nohwnd/AssertAssert-Equivalent库中的一个cmdlet看起来很有希望。

于 2019-11-04T13:23:22.970 回答
0

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


$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 '==' }
    }
}

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

于 2021-03-28T00:44:00.383 回答