1

I wrote a unit test in pester where it verifies the result against the returned object (a hash table) created using PSCustomObject, but I am not sure how to definite it:

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

after invoking pester, I get:

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

it technically was the correct value I wanted, but not sure how to definite the last portion of the test

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
RSLAV
  • 53
  • 6

3 Answers3

0

You need to compare the underlying types, because the comparison you have today is comparing the whole object you have in $result versus the Class [PSObject], which won't work. Instead, try this.

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

Either syntax should work, Mr. Vance.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
0

I should have provided more detail, but I discovered a resolution that involved creating an mock that returned a hash table with the values I required to test. Meaning, I could actually verify the output for certain scenarios. Thanks again.

RSLAV
  • 53
  • 6
0

Working solution to check the actual is of expected type:

Describe "return type" {
  It "should have type [pscustomobject]" {
    $actual = [pscustomobject]@{a = 1}
    $actual | Should -BeOfType [pscustomobject]
  }
}

Checked with PS7.0.3 and Pester 5.0.4

If you want to check the object has only expected properties and values, then you can do it this way:

# 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
  }
}
Ivan Akcheurov
  • 2,173
  • 19
  • 15