You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mocking a function that has a parameter of type [System.Collections.Specialized.OrderedDictionary] throws an error when using PowerShell 7 (version 7.3.4) but runs fine when using PowerShell 5.
See test script in attached file (hehe.txt). hehe.txt
Expected Behavior
Pester should not throw an error. If it works in PowerShell 5 it should also work in PowerShell 7.
Steps To Reproduce
Running below tests in PowerShell 7 throws an error. They run fine with PowerShell 5.
Describe 'Difference between PowerShell 7 and PowerShell 5' {
BeforeAll {
function Test-WithOrderedDictionary {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[System.Collections.Specialized.OrderedDictionary]
$Context
)
Write-Information -MessageData "INFO -- Test-WithOrderedDictionary: $($Context.Message)" -InformationAction Continue
}
function Use-TestWithOrderedDictionary {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$Message
)
Write-Information -MessageData "INFO -- Use-TestWithOrderedDictionary: $Message" -InformationAction Continue
$testContext = [ordered]@{
Message = $Message
}
Test-WithOrderedDictionary -Context $testContext
}
function Test-WithHashTable {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[hashtable]
$Context
)
Write-Information -MessageData "INFO -- Test-WithHashTable: $($Context.Message)" -InformationAction Continue
}
function Use-TestWithHashTable {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$Message
)
Write-Information -MessageData "INFO -- Use-TestWithHashTable: $Message" -InformationAction Continue
$testContext = @{
Message = $Message
}
Test-WithHashTable -Context $testContext
}
}
Context 'Test-WithOrderedDictionary' {
It 'With mock throws error with PowerShell 7, but works with PowerShell 5' {
Mock -CommandName 'Test-WithOrderedDictionary'
Use-TestWithOrderedDictionary -Message 'With mock throws error with PowerShell 7, but works with PowerShell 5'
}
It 'Without mock runs fine with PowerShell 7 and PowerShell 5' {
Use-TestWithOrderedDictionary -Message 'Without mock runs fine with PowerShell 7 and PowerShell 5'
}
}
Context 'Test-WithHashTable' {
It 'With mock runs fine with PowerShell 7 and PowerShell 5' {
Mock -CommandName 'Test-WithHashTable'
Use-TestWithHashTable -Message 'With mock runs fine with PowerShell 7 and PowerShell 5'
}
It 'Without mock runs fine with PowerShell 7 and PowerShell 5' {
Use-TestWithHashTable -Message 'Without mock runs fine with PowerShell 7 and PowerShell 5'
}
}
}
Describe your environment
Pester version : 5.4.1 C:\Program Files\PowerShell\Modules\Pester\5.4.1\Pester.psm1
PowerShell version : 7.3.4
OS version : Microsoft Windows NT 10.0.19044.0
Possible Solution?
It's not a solution but as a workaround one can use a hashtable instead of an ordered dictionary.
The text was updated successfully, but these errors were encountered:
Unfortunately I'm actually the one who introduced this bug in PS 7.3 by making [ordered] a proper type accelerator in PowerShell/PowerShell#17804. It seems the proxy command generator prefers the accelerator now, but that will fail as [ordered] has a special guard to block it from being used like this. That's something the working group decided to keep as-is, though not with this scenario in mind, see PowerShell/PowerShell#10513 (comment).
I'll submit an issue in the PowerShell-repo so we hopefully can fix this in a future PowerShell-release.
Checklist
What is the issue?
Mocking a function that has a parameter of type [System.Collections.Specialized.OrderedDictionary] throws an error when using PowerShell 7 (version 7.3.4) but runs fine when using PowerShell 5.
See test script in attached file (hehe.txt).
hehe.txt
Expected Behavior
Pester should not throw an error. If it works in PowerShell 5 it should also work in PowerShell 7.
Steps To Reproduce
Running below tests in PowerShell 7 throws an error. They run fine with PowerShell 5.
Describe your environment
Pester version : 5.4.1 C:\Program Files\PowerShell\Modules\Pester\5.4.1\Pester.psm1
PowerShell version : 7.3.4
OS version : Microsoft Windows NT 10.0.19044.0
Possible Solution?
It's not a solution but as a workaround one can use a hashtable instead of an ordered dictionary.
The text was updated successfully, but these errors were encountered: