这是我的代码: Testcase.ps1
function Verify_AuthenticodeSignature{
Param(
[Parameter(Mandatory=$true)]
[string]$PSFilePath
)
Write-Host $PSFilePath
if (-Not (Test-Path $PSFilePath)){
echo "file does not exist"
exit 1
}
$Status=(Get-AuthenticodeSignature -FilePath $PSFilePath).Status
if($Status -ne "Valid"){
echo "Script is not signed"
}
}
Verify_AuthenticodeSignature
测试用例.Tests.ps1:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
Describe "Testing" {
It "Test Path"{
Mock Test-Path{$false}
Mock echo{}
Verify_AuthenticodeSignature
Assert-MockCalled echo -Times 1
#Verify_AuthenticodeSignature -PSFilePath 'D:\abc\xyz.ps1' |should be "file does not exist"
}
It "Authentication"{
Mock Get-AuthenticodeSignature{ return "valid" }
Mock echo{}
Verify_AuthenticodeSignature
Assert-MockCalled Get-AuthenticodeSignature -Times 1
Assert-MockCalled echo -Times 1
}
}
在这里,我想模拟一个带有强制参数的 powershell 函数,它不会向用户询问 $PSFilePath 变量值,而是使用任何虚拟值检查模拟函数。每当我运行Testcase.Tests.ps1时,它都会提示输入 $PSFilePath 值并运行源 powershell 脚本(Testcase.ps1)我被困在这个问题上,任何建议都会有很大帮助。