如何使用 Pester 将我的函数正确模拟为 return 自定义 属性?
How do I correctly mock my Function to return a custom property with Pester?
我对 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
模拟为 return true 以便代码路径进入 if
块内,但是结果对 [= 没有任何价值16=]。
根据代码路径,我的测试总是进入默认块。我试过
创建自定义对象 $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."
}
}
有几个问题:
- 根据 4c 的评论,您的 Mock 可能不会因为范围界定而被调用(除非您的上下文周围有一个未显示的描述块)。如果将
Context
更改为 Describe
,然后使用 Assert-VerifiableMocks
,您可以看到 Mock 确实会被调用。
- 您无法验证使用
Write-Host
的代码的输出,因为此命令不会写入正常输出流(它会写入主机控制台)。如果删除 Write-Host
以便将字符串返回到标准输出流,则代码有效。
- 您可以按照您的建议使用
[PSCustomObject]@{Progress = 0}
模拟 .Progress
属性 的输出,但我相信这应该在 Get-Status
的 Mock 中。
这是一个有效的 minimal/verifiable 示例:
$Name = 'SomeName'
#Had to define an empty function in order to be able to Mock it. You don't need to do this in your code as you have the real function.
Function Get-Status { }
#I assumed based on your code all of this code was wrapped as a Function called Confirm-Session
Function Confirm-Session {
$State = Get-Status
if ($State) {
switch ($State.Progress) {
0 {
"Session for $Name not initiated. Retrying."
}
100{
"Session for $Name at $($State.Progress) percent"
}
default {
"Session for $Name in progress (at $($State.Progress) percent)."
}
}
}
}
#Pester tests for the above code:
Describe 'State Progress returns 0' {
mock Get-Status {
[PSCustomObject]@{Progress = 0}
} -Verifiable
#$State = [PSCustomObject]@{Progress = 0}
$result = Confirm-Session
it 'should be' {
$result | should be "Session for $Name not initiated. Retrying."
}
it 'should call the verifiable mocks' {
Assert-VerifiableMocks
}
}
Returns:
Describing State Progress returns 0
[+] should be 77ms
[+] should call the verifiable mocks 7ms
我对 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
模拟为 return true 以便代码路径进入 if
块内,但是结果对 [= 没有任何价值16=]。
根据代码路径,我的测试总是进入默认块。我试过
创建自定义对象 $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."
}
}
有几个问题:
- 根据 4c 的评论,您的 Mock 可能不会因为范围界定而被调用(除非您的上下文周围有一个未显示的描述块)。如果将
Context
更改为Describe
,然后使用Assert-VerifiableMocks
,您可以看到 Mock 确实会被调用。 - 您无法验证使用
Write-Host
的代码的输出,因为此命令不会写入正常输出流(它会写入主机控制台)。如果删除Write-Host
以便将字符串返回到标准输出流,则代码有效。 - 您可以按照您的建议使用
[PSCustomObject]@{Progress = 0}
模拟.Progress
属性 的输出,但我相信这应该在Get-Status
的 Mock 中。
这是一个有效的 minimal/verifiable 示例:
$Name = 'SomeName'
#Had to define an empty function in order to be able to Mock it. You don't need to do this in your code as you have the real function.
Function Get-Status { }
#I assumed based on your code all of this code was wrapped as a Function called Confirm-Session
Function Confirm-Session {
$State = Get-Status
if ($State) {
switch ($State.Progress) {
0 {
"Session for $Name not initiated. Retrying."
}
100{
"Session for $Name at $($State.Progress) percent"
}
default {
"Session for $Name in progress (at $($State.Progress) percent)."
}
}
}
}
#Pester tests for the above code:
Describe 'State Progress returns 0' {
mock Get-Status {
[PSCustomObject]@{Progress = 0}
} -Verifiable
#$State = [PSCustomObject]@{Progress = 0}
$result = Confirm-Session
it 'should be' {
$result | should be "Session for $Name not initiated. Retrying."
}
it 'should call the verifiable mocks' {
Assert-VerifiableMocks
}
}
Returns:
Describing State Progress returns 0
[+] should be 77ms
[+] should call the verifiable mocks 7ms