我对 PowerShell 尤其是 Pester 测试有点陌生。我似乎无法为我正在进行 Pester 测试的功能重新创建场景。
这是代码:
$State = Get-Status
if(State) {
switch ($State.Progress) {
0 {
Write-Host "Session for $Name not initiated. Retrying."
}
100{
Write-Host "Session for $Name at $($State.Progress) percent"
}
default {
Write-Host "Session for $Name in progress (at $($State.Progress)
percent)."
}
}
我已经模拟Get-Status
返回 true 以便代码路径进入if
块内,但是结果对于$State.Progress
.
就代码路径而言,我的测试总是会进入默认块。我尝试创建自定义对象$State = [PSCustomObject]@{Progress = 0}
无济于事。
这是我的 Pester 测试的一部分:
Context 'State Progress returns 0' {
mock Get-Status {return $true} -Verifiable
$State = [PSCustomObject]@{Progress = 0}
$result = Confirm-Session
it 'should be' {
$result | should be "Session for $Name not initiated. Retrying."
}
}