0

still trying to learn Pester, I got a snippet from ChatGPT:

function Get-EvenNumbers {
param(
[Parameter(Mandatory=$true)]
[int]$start,

[Parameter(Mandatory=$true)]
[int]$end
)

$evenNumbers = @()

for ($i = $start; $i -le $end; $i++) {
    if ($i % 2 -eq 0) {
      $evenNumbers += $i
    }
}

return $evenNumbers
}

Pester-code looks like:

Describe "Get-EvenNumbers" {
Context "When the input range is from 1 to 10" {
$start = 1
$end = 10

It "Returns an array of even numbers" {
  $result = Get-EvenNumbers -Start $start -End $end
  $result | Should -BeOfType "System.Array"
  $result | Should -Not -BeNullOrEmpty
  $result | Should -ContainOnly (2, 4, 6, 8, 10)
   }
}

Context "When the input range is from 2 to 9" {
$start = 2
$end = 9

It "Returns an array of even numbers" {
  $result = Get-EvenNumbers -Start $start -End $end
  $result | Should -BeOfType "System.Array"
  $result | Should -Not -BeNullOrEmpty
  $result | Should -ContainOnly (2, 4, 6, 8)
     }
    }
}

and get an error: "[-] Get-EvenNumbers.When the input range is from 1 to 10.Returns an array of even numbers 10ms (8ms|1ms) Expected the value to have type [array] or any of its subtypes, but got 2 with type [int]. at $result | Should -BeOfType "System.Array", C:\Users\Pester\Get-EvenNumbers.Tests.ps1:8 at , C:\Users\Pester\Get-EvenNumbers.Tests.ps1:8 [-] Get-EvenNumbers.When the input range is from 2 to 9.Returns an array of even numbers 13ms (9ms|4ms) Expected the value to have type [array] or any of its subtypes, but got 2 with type [int]. at $result | Should -BeOfType "System.Array", C:\Users\Pester\Get-EvenNumbers.Tests.ps1:20 at , C:\Users\Pester\Get-EvenNumbers.Tests.ps1:20 Tests completed in 177ms Tests Passed: 0, Failed: 2, Skipped: 0 NotRun: 0" But the return value of the function is an [int]array?

1

1 Answer 1

3

Arrays are enumerated when passed through the pipeline, in your example Should is comparing the type of each element of the array and not the array as a whole:

"...but got 2 with type [int]..."

You can use the comma operator , to make your test succeed but note that Should is meant testing the type of an element not to test an array as a whole.

For example, you could define your test as follows:

It 'Should be an integer equal to 2 as first element' {
    $result = Get-EvenNumbers -Start $start -End $end |
        Select-Object -First 1

    $result | Should -BeOfType [int]
    $result | Should -BeExactly 2
}

Or you could also define your test as:

It 'Should return an array of only integers' {
    $result = Get-EvenNumbers -Start $start -End $end
    $result | Should -BeOfType [int]
    $result | Should -BeGreaterThan 1
}

Also your function should be defined in the BeforeAll block and the ranges, $start and $end, should be defined either in the BeforeEach block or inside the It block of each test.

Lastly, not sure what you meant by -ContainOnly, there is no such parameter for Should.

BeforeAll {
    function Get-EvenNumbers {
        param(
            [Parameter(Mandatory=$true)]
            [int] $start,

            [Parameter(Mandatory=$true)]
            [int] $end
        )

        for ($i = $start; $i -le $end; $i++) {
            if ($i % 2 -eq 0) {
                $i
            }
        }
    }
}
Describe 'Get-EvenNumbers' {
    Context 'When the input range is from 1 to 10' {
        BeforeEach {
            $start = 1
            $end = 10
        }
        It 'Returns an array of even numbers' {
            $result = Get-EvenNumbers -Start $start -End $end
            , $result | Should -BeOfType [array]
            $result | Should -Not -BeNullOrEmpty
            $result | Should -BeExactly 2, 4, 6, 8, 10
        }
    }

    Context 'When the input range is from 2 to 9' {
        BeforeEach {
            $start = 2
            $end = 9
        }
        It 'Returns an array of even numbers' {
            $result = Get-EvenNumbers -Start $start -End $end
            , $result | Should -BeOfType [array]
            $result | Should -Not -BeNullOrEmpty
            $result | Should -BeExactly 2, 4, 6, 8
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.