Is there any way to mock the $PSVersionTable
variable of PowerShell with Pester?
Here is my code:
Testcase.ps1:
trap [Exception] {
write-error $("TRAPPED: " + $_)
exit 1
}
function Check_Powershell_Version
{
$PSVersion = $PSVersionTable.PSVersion.Major
if ($PSVersion -lt 3){
echo "Current version is not compatible. Please upgrade powershell"
}
}
Testcase.Tests.ps1:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
Describe "SRP testing" {
It "Check Powershell Version" {
Mock $PSVersionTable {
return{ @{ PSVersion = [Version]'1.0.0' } }
}
Check_Powershell_Version
Assert-MockCalled $PSVersionTable -Times 1
}
}
I want to mock the $PSVersionTable
variable so that I can check that it has been called.