-
-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem using HaveCount on [hashtable] #1234
Comments
I have updated the issue for the Pester 5.1.1 where the issue still exist. It is also not possible to use Describe 'Get-HashTable' {
Context 'When returning a hashtable' {
It 'Test' {
$getHashTableResult = @{
Property1 = '1'
Property2 = '3'
}
Should -ActualValue $getHashTableResult -HaveCount 2
}
}
} |
It is not possible to use Describe 'Get-HashTable' {
Context 'When returning a hashtable' {
It 'Test' {
$getHashTableResult = @{
Property1 = '1'
Property2 = '3'
}
Should -ActualValue $getHashTableResult.Keys -HaveCount 2
}
}
}
|
You can use A hashtable is considered a single item when passed through the pipeline (and in a foreach-loop), so you would need
|
I good with closing this issue. If |
Stumbled upon this issue with Pester 5.1.1: Describe 'a hash table should be correctly counted with' {
BeforeAll {
$hashTable = @{
Key1 = 1
Key2 = 2
}
}
It '-BeExactly' {
$hashTable.Count | Should -BeExactly 2
}
It '-HaveCount' {
$hashTable | Should -HaveCount 2 # FAIL
}
}
Describe 'an array should be correctly counted with' {
BeforeAll {
$array = @(1, 3)
}
It '-BeExactly' {
$array.Count | Should -BeExactly 2
}
It '-HaveCount' {
$array | Should -HaveCount 2
}
} It would be great if Pester would simply check the type and if it's a hasthtable check how many keys there are. @nohwnd , is this something that would be considered? |
Having also stumbled over this recently too, I have to agree. From a usability point of view, it's very confusing: You're testing an object that has a |
@iambic69 No disagreement here, but sometimes there are technical limitations. The problem is that due to the pipeline-behavior in PowerShell which doesn't enumerate hashtables-entries unlike other # Would fail due to count of 2 keys and not 1 hashtable as expected
@(@{"one"=1,"two"=2}) | Should -HaveCount 1 That is unless someone has an idea of how to properly detect "a single dictionary" vs "array with single dictionary" through the pipeline. |
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).2. Describe Your Environment
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.
5. Possible Solution
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).6. Context
Very minor inconvenience. Having
HaveCount
would just make the code prettier. Workaround is using the following instead.The text was updated successfully, but these errors were encountered: