根据objectsarray的相等性,我们正在努力使Pester测试失败或通过。

Test.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仅仅是另一个对象的属性。因此,需要进行深度比较。

最佳答案

当我想将某些“复杂”对象与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)
        }
    }
}

关于arrays - 如何比较两个数组与Pester中的自定义对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50870891/

10-11 08:38