Open
Description
1. General summary of the issue
When using HaveCount
on a hashtable, the count is always 1. The following snippet shows the problem (both in Windows PowerShell and PowerShell Core).
Describe 'Get-HashTable' {
Context 'When returning a hashtable' {
It 'Test' {
$getHashTableResult = @{
Property1 = '1'
Property2 = '3'
}
$getHashTableResult | Should -BeOfType [System.Collections.Hashtable]
$getHashTableResult | Should -HaveCount 2
}
}
}
2. Describe Your Environment
Pester version : 5.1.1 C:\source\SqlServerDsc\output\RequiredModules\Pester\5.1.1\Pester.psm1
PowerShell version : 7.1.0
OS version : Microsoft Windows NT 10.0.19042.0
3. Expected Behavior
HaveCount
should report the correct number of properties in the hashtable.
4.Current Behavior
Fails with an error even though there are two properties in the hashtable.
Starting discovery in 1 files.
Discovery finished in 22ms.
[-] Get-HashTable.When returning a hashtable.Test 8ms (7ms|1ms)
Expected a collection with size 2, but got collection with size 1 @(System.Collections.Hashtable).
at $getHashTableResult | Should -HaveCount 2, C:\Users\johan.ljunggren\Desktop\Bug_HaveCount.ps1:10
5. Possible Solution
NOTE: The below screenshots are from Pester 4.x, but the same issue still exist in 5.1.1.
The problem seems that it compares the wrong array. Instead of checking $ActualValue.Count
, it should check $ActualValue[0].Count
(at least in this case).
[DBG]> $ActualValue.Count
1
[DBG]> $ActualValue[0].Count
2
6. Context
Very minor inconvenience. Having HaveCount
would just make the code prettier. Workaround is using the following instead.
$getHashTableResult.Count | Should -Be 2