-
-
Notifications
You must be signed in to change notification settings - Fork 475
Description
1. Provide a general summary of the issue in the Title above
Pester 4.2.0 introduced a new -HasCount
assertion operator. When migrating existing tests, by replacing $variable.Count | Should -Be $expectedCount
to $variable | Should -HaveCount $expectedCount
, I found edge cases where this does not work. I created Pester tests to demonstrate this behaviour in a simple case but later on upon request I can also give 2 more comples examples where -HasCount
does not work. The idea is to first investigate/fix the first simple problem and only afterwards approach the more complex test cases.
Describe "Pesters -HaveCount operator should not fail on `$null object that has Count property" {
It "when not using strong typing" {
$aliases = Get-Alias | Select-Object -First 0
$aliases.Count | Should -Be 0
$aliases | Should -HaveCount 0
}
It "when LHS is casted to [System.Array]" {
[System.Array] $aliases = Get-Alias | Select-Object -First 0
$aliases.Count | Should -Be 0
$aliases | Should -HaveCount 0
}
It "when RHS is casted to [System.Array]" {
$aliases = [System.Array] (Get-Alias | Select-Object -First 0)
$aliases.Count | Should -Be 0
$aliases | Should -HaveCount 0
}
It "when LHS and RHS are casted to [System.Array]" {
[System.Array] $aliases = [System.Array] (Get-Alias | Select-Object -First 0)
$aliases.Count | Should -Be 0
$aliases | Should -HaveCount 0
}
}
2. Describe Your Environment
Pester version : 4.3.1
PowerShell version : 5.1.16299.248
PowerShell version : 6.0.1
PowerShell version : 6.1.0-preview.589
OS version : Microsoft Windows NT 10.0.16299.0
3. Expected Behavior
All 4 tests above should pass
4.Current Behavior
The first test passed but not the other 3. By debugging it, the objects seem to be of the same type ($null
), therefore I cannot explain why Pester behaves differently. PowerShell has .Count
property on $null
by design.
Describing Pesters -HaveCount operator should not fail on $null object that has Count property
[+] when not using strong typing 387ms
[-] when LHS is casted to [System.Array] 132ms
Expected an empty collection, but got collection with size 1 @().
11: $aliases | Should -HaveCount 0
at <ScriptBlock>, C:\Users\cberg\git\bla.tests.ps1: line 11
[-] when RHS is casted to [System.Array] 105ms
Expected an empty collection, but got collection with size 1 @().
17: $aliases | Should -HaveCount 0
at <ScriptBlock>, C:\Users\cberg\git\bla.tests.ps1: line 17
[-] when LHS and RHS are casted to [System.Array] 37ms
Expected an empty collection, but got collection with size 1 @().
23: $aliases | Should -HaveCount 0
at <ScriptBlock>, C:\Users\cberg\git\bla.tests.ps1: line 23
Tests completed in 662ms
Tests Passed: 1, Failed: 3, Skipped: 0, Pending: 0, Inconclusive: 0
5. Possible Solution
Document more clearly on what types the new HasCount
assertion operator works but to me it rather looks like a Pester internal bug that needs to be resolved.
6. Context
As described in paragraph: migrating to new -HasCount
operator, i.e. not a blocking issue.