我有一个简单的问题,希望有人能帮上忙。
我用pester编写了一个单元测试,它根据使用PSCustomObject创建的返回对象(哈希表)来验证结果,但我不确定如何定义它:
$result = get-dataFromOverThere
$result | Should -Be [PSObject]在调用pester之后,我得到:
Expected '[PSCustomObject]', but got @{ name = "bob"; company = "vance refrigeration"}.从技术上讲,这是我想要的正确值,但不确定如何定义测试的最后一部分
发布于 2020-06-04 10:41:38
您需要比较底层类型,因为您现在的比较是将$result中的整个对象与[PSObject]类进行比较,这是不起作用的。相反,你可以试试这个。
$result.GetType().Name | should -be 'PSCustomObject'
#or
$result.GetType().Name -eq 'PSCustomObject' | should -be $true 两种语法都应该行得通万斯先生。
发布于 2020-06-05 15:34:29
我本应该提供更多细节,但我发现了一个解决方案,其中包括创建一个模拟,该模拟返回一个哈希表,其中包含我需要测试的值。这意味着,我可以实际验证某些场景的输出。再次感谢。
发布于 2020-09-21 22:47:04
检查实际的工作解决方案是预期的类型
Describe "return type" {
It "should have type [pscustomobject]" {
$actual = [pscustomobject]@{a = 1}
$actual | Should -BeOfType [pscustomobject]
}
}使用PS7.0.3和Pester 5.0.4检查
如果您想要检查对象是否只具有预期的属性和值,则可以这样做:
# Install-Module Functional
Import-Module Functional
Describe "return values" {
It "checks that the actual object has correct properties and values" {
$actual = [pscustomobject]@{a = 1}
$expected = [pscustomobject]@{"a" = 1}
$actual, $expected | Test-Equality | Should -BeTrue
}
}https://stackoverflow.com/questions/62185646
复制相似问题