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?