由于脚本块,无法达到 100% 纠缠代码覆盖率
Can not reach 100% Pester Code Coverage due to Script block
大家好,
由于脚本块,我的纠缠者没有达到 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 时调用它-统计数据。我的 Pester 成功了,但代码覆盖率没有达到 100%。
你们能告诉我如何做到这一点吗?我需要改变我的测试吗?
谢谢
在 Describe {
块的开头,包含 Invoke-Command
:
的模拟
Describe '..' {
Mock Invoke-Command { Param($SB) . $SB }
...
}
大家好,
由于脚本块,我的纠缠者没有达到 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 时调用它-统计数据。我的 Pester 成功了,但代码覆盖率没有达到 100%。 你们能告诉我如何做到这一点吗?我需要改变我的测试吗? 谢谢
在 Describe {
块的开头,包含 Invoke-Command
:
Describe '..' {
Mock Invoke-Command { Param($SB) . $SB }
...
}