大家好,
由于脚本块,我的纠缠没有达到 100% 的代码覆盖率,我一直在苦苦挣扎。我一直在研究和阅读文章,但没有成功,我决定向你们寻求帮助。:)
我要描述的部分代码包含一个脚本块,稍后将提供给 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 块(Get-Statistics)中创建了一个自定义函数,它基本上是脚本块的内部,每当我将 Invoke-Command 模拟为 Get-Statistics 时都会调用它。我的 Pester 成功了,但代码覆盖率并没有给我 100%。你们能告诉我如何做到这一点吗?我需要改变我的测试吗?谢谢你