【问题标题】:Pester test evaluating PSCustomObject - unablePester 测试评估 PSCustomObject - 无法
【发布时间】:2020-09-21 22:47:03
【问题描述】:

我有一个简单的问题,希望有人能提供帮助。

我在 pester 中编写了一个单元测试,它根据使用 PSCustomObject 创建的返回对象(哈希表)验证结果,但我不确定如何确定它:

$result = get-dataFromOverThere
$result | Should -Be [PSObject]

调用pester后,我得到:

Expected '[PSCustomObject]', but got @{ name = "bob"; company = "vance refrigeration"}.

从技术上讲,它是我想要的正确值,但不确定如何确定测试的最后一部分

【问题讨论】:

  • 嗨,$result -is [PSObject] 是你所追求的

标签: powershell pester


【解决方案1】:

您需要比较基础类型,因为您今天的比较是比较您在$result 中的整个对象与[PSObject] 类,这是行不通的。相反,试试这个。

$result.GetType().Name | should -be 'PSCustomObject'
#or
$result.GetType().Name -eq 'PSCustomObject' | should -be $true 

任何一种语法都应该可以,万斯先生。

【讨论】:

    【解决方案2】:

    我应该提供更多详细信息,但我发现了一个解决方案,该解决方案涉及创建一个模拟,该模拟返回一个包含我需要测试的值的哈希表。意思是,我实际上可以验证某些场景的输出。再次感谢。

    【讨论】:

      【解决方案3】:

      检查实际是否属于预期类型的工作解决方案:

      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
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-04
        • 1970-01-01
        • 2014-02-05
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多