有没有办法$PSVersionTable
用 Pester 模拟 PowerShell 的变量?
这是我的代码:
测试用例.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"
}
}
测试用例.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
}
}
我想模拟$PSVersionTable
变量,以便我可以检查它是否已被调用。