node.js - 使用 Pester 覆盖 powershell 脚本

标签 node.js powershell pester

我正在尝试使用 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/

相关文章:

Powershell 单元测试用例

node.js - forEach 里面的 Promise

node.js - 在 Firebase 中插入 JSON 显示为字符串而不是显示为树

node.js - 国际电影节。 TypeError : require(. ..)(...) 不是函数

powershell - 可以在我的一台(客户端)计算机上执行 Select-AzureRmSubscription,但不能在另一台计算机上执行

powershell - 尝试在包含大量文件的目录中获取唯一的扩展名列表非常缓慢

Powershell - 如何将 <sectionGroup> 添加到 web.config

mongodb - 将 kubectl exec 的返回值获取到 powershell 脚本中

powershell - 由于脚本 block ,无法达到 100% 纠缠代码覆盖率

c++ - OpenSSL:EC_POINT_set_compressed_coordinates_GFp 段错误