gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:38:37 25 4
gpt4 key购买 nike

大家好,

由于脚本 block ,我的纠缠者没有达到 100% 的代码覆盖率,我为此苦苦挣扎了很长时间。我一直在研究和阅读文章但没有成功,我决定向你们寻求帮助。 :)

我想描述的部分代码包含一个脚本 block ,稍后将被提供给一个 cmdlet Invoke-Command。示例代码如下:

function Get-Function {
.....
Set-Alias Start-PowerShell32 $env:computername\WindowsPowerShell\v1.0\powershell.exe"
$ScriptBlock = {
Start-PowerShell32 -noprofile {
$sample = Get-Content -Path "C:\sample.txt
Import-Module "C:\Program Files (x86)\Software"

@($sample) | Get-Service | Where-Object { $_.Status -eq 'Stopped' }
}
}
Invoke-Command -ScriptBlock $ScriptBlock
}

Describe Get-Function {
....
function Get-Statistics {

Start-PowerShell32 -noprofile {
$sample = Get-Content -Path "C:\sample.txt
Import-Module "C:\Program Files (x86)\Software"

@($sample) | Get-Service | Where-Object { $_.Status -eq 'Stopped' }
}
}
Context '1st Context'{
mock Set-Alias {'Setting alias for Powershell 32 bit'} -Verifiable
mock Get-Statistics {'Getting Statistics ....'} -Verifiable
mock Invoke-Command {Get-Statistics} -Verifiable
$result = Get-Function

it 'should return etting alias for Powershell 32 bit'{
$result[0] | should be "Setting alias for Powershell 32 bit"
}
it 'should return Getting Mailbox Statistics ....'{
$result[1] | should be "Getting Statistics ...."
}
it 'should call all verifiable mocks'{
Assert-VerifiableMocks
}
}
}

我在我的 Pester 上所做的是,我在我的 Describe block (Get-Statistics)中创建了一个自定义函数,它基本上是脚本 block 的内部,以便在我将 Invoke-Command 模拟为 Get-Statistics 时调用它.我的 Pester 成功了,但代码覆盖率没有达到 100%。你们能告诉我如何做到这一点吗?我需要改变我的测试吗?谢谢你

最佳答案

Describe { block 的开头,包含一个用于 Invoke-Command 的模拟:

Describe '..' {
Mock Invoke-Command { Param($SB) . $SB }

...
}

关于powershell - 由于脚本 block ,无法达到 100% 纠缠代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47677420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com