它在 Pester 中执行函数时跳过 it-statement,但不应该
It skips it-statement when executing function in Pester, but shouldn't
我正在尝试测试正在测试与 PC 的连接的脚本文件中的函数。我试图通过在纠缠测试中从其他脚本调用模拟“测试连接”来做到这一点。
当我运行Temp.Testing.ps1
describe 'Test Error Handling' {
$myDir = Split-Path -parent $PSCommandPath
$testFile = "$myDir\TryToRenameComputer.ps1"
.$testFile
mock 'Test-Connection' { $false }
$pathForLogs = "C:\temp\Logs.txt"
it 'exits if Test-Connection Failed'{
TryToRenameComputer -OldName "OldName"
Assert-MockCalled 'Test-Connection' -Times 1 -Scope It
}
}
尝试重命名计算机。ps1
function TryToRenameComputer {
param([parameter(Mandatory=$true)]
[string]$computerName)
if (!(Test-Connection -ComputerName $computerName -Quiet)) {
exit
}
}
它跳过了 it 语句并且没有显示任何错误。只有“描述测试错误处理”。
预期结果:
实际结果:
我已经尝试 运行使用其他函数并且它起作用了。
此外,当我 运行 宁多个 it 语句时,当我在 1 个 it 语句中调用函数时,所有 it 语句都会被跳过。
我也试过重写它,所以它不再起作用了,但它起作用了。
问题很可能是由于代码中的 exit
语句引起的。这是被执行的,因为在它之前的 if
语句中,您通过其 !
的 shorthand 使用 -not
来测试 Test-Connection
的结果,因为您Mock
将 Test-Connection
设置为 $false
。
通过使用 exit
,您将立即终止 PowerShell 主机,然后它会停止来自 executing/completing 的测试。
而不是使用 exit
考虑使用 break
或 return
来停止函数的执行而不终止脚本。或者,如果您可能确实想要终止脚本,请考虑使用 throw
,因为您可以在发生异常时停止父脚本。
然后您可以修改您的测试以测试 throw
,因为这是您期望在 test-connection
returns $false
时出现的结果。例如:
function TryToRenameComputer {
param([parameter(Mandatory=$true)]
[string]$computerName)
if (!(Test-Connection -ComputerName $computerName -Quiet)) {
Thow "Could not connect to $computerName"
}
}
describe 'Test Error Handling' {
$myDir = Split-Path -parent $PSCommandPath
$testFile = "$myDir\TryToRenameComputer.ps1"
.$testFile
mock 'Test-Connection' { $false }
$pathForLogs = "C:\temp\Logs.txt"
it 'Throws an error if Test-Connection Failed'{
{ TryToRenameComputer -OldName "OldName" } | Should -Throw
Assert-MockCalled 'Test-Connection' -Times 1 -Scope It
}
}
在您的测试中没有简单的方法来处理 exit
,并且在您编写自动化代码时通常有点 anti-pattern。
我正在尝试测试正在测试与 PC 的连接的脚本文件中的函数。我试图通过在纠缠测试中从其他脚本调用模拟“测试连接”来做到这一点。
当我运行Temp.Testing.ps1
describe 'Test Error Handling' {
$myDir = Split-Path -parent $PSCommandPath
$testFile = "$myDir\TryToRenameComputer.ps1"
.$testFile
mock 'Test-Connection' { $false }
$pathForLogs = "C:\temp\Logs.txt"
it 'exits if Test-Connection Failed'{
TryToRenameComputer -OldName "OldName"
Assert-MockCalled 'Test-Connection' -Times 1 -Scope It
}
}
尝试重命名计算机。ps1
function TryToRenameComputer {
param([parameter(Mandatory=$true)]
[string]$computerName)
if (!(Test-Connection -ComputerName $computerName -Quiet)) {
exit
}
}
它跳过了 it 语句并且没有显示任何错误。只有“描述测试错误处理”。
预期结果:
实际结果:
我已经尝试 运行使用其他函数并且它起作用了。 此外,当我 运行 宁多个 it 语句时,当我在 1 个 it 语句中调用函数时,所有 it 语句都会被跳过。 我也试过重写它,所以它不再起作用了,但它起作用了。
问题很可能是由于代码中的 exit
语句引起的。这是被执行的,因为在它之前的 if
语句中,您通过其 !
的 shorthand 使用 -not
来测试 Test-Connection
的结果,因为您Mock
将 Test-Connection
设置为 $false
。
通过使用 exit
,您将立即终止 PowerShell 主机,然后它会停止来自 executing/completing 的测试。
而不是使用 exit
考虑使用 break
或 return
来停止函数的执行而不终止脚本。或者,如果您可能确实想要终止脚本,请考虑使用 throw
,因为您可以在发生异常时停止父脚本。
然后您可以修改您的测试以测试 throw
,因为这是您期望在 test-connection
returns $false
时出现的结果。例如:
function TryToRenameComputer {
param([parameter(Mandatory=$true)]
[string]$computerName)
if (!(Test-Connection -ComputerName $computerName -Quiet)) {
Thow "Could not connect to $computerName"
}
}
describe 'Test Error Handling' {
$myDir = Split-Path -parent $PSCommandPath
$testFile = "$myDir\TryToRenameComputer.ps1"
.$testFile
mock 'Test-Connection' { $false }
$pathForLogs = "C:\temp\Logs.txt"
it 'Throws an error if Test-Connection Failed'{
{ TryToRenameComputer -OldName "OldName" } | Should -Throw
Assert-MockCalled 'Test-Connection' -Times 1 -Scope It
}
}
在您的测试中没有简单的方法来处理 exit
,并且在您编写自动化代码时通常有点 anti-pattern。