我正在尝试使用 Pester 使用以下代码片段对我的 Powershell 脚本文件进行单元测试
#code to create a 7z file
$7zipPath = "C:\Program Files\7-Zip\7z.exe"
Set-Alias 7zip $7zipPath
if (!(Test-Path -Path $7zipPath -PathType Leaf)) {
throw "7 zip file '$7zipPath' not found"
}
7zip a -mx=9 $jenkinsWorkspacepath\IntegrationZip.7z $jenkinsWorkspacepath\IntegratedScripts
并模拟 Test-Path 命令如下
Mock -CommandName Test-Path –MockWith {
Return $false
}
但是覆盖率报告显示以下行未被覆盖。我在这里做错了什么(在模拟部分)?
**throw "7 zip file '$7zipPath' not found"**
最佳答案
你的测试是什么样的?我尝试了以下方法,它对您的代码运行良好:
Mock -CommandName Test-Path -MockWith { return $false }
It "fails to find executable" {
{ Invoke-SevenZip } | Should -Throw "not found"
}
注意:我将您的代码包装在名为 Invoke-SevenZip
关于node.js - 使用 Pester 覆盖 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60277558/