当我运行以下纠缠测试时,我希望它能够捕获预期的错误,但事实并非如此。但是,当我使用不同的函数和不同的 throw 语句运行测试时,它会起作用。
纠缠测试:
Describe "Remove-GenericCredential Function Tests" {
$InternetOrNetworkAddress = 'https://[email protected] /cdn-cgi/l/email-protection'
Context "Test: Remove-GenericCredential -InternetOrNetworkAddress '$InternetOrNetworkAddress' (Credential does not exist)" {
It "This Command threw an error. The credential does not exist." { { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false) } | should throw "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found" }
}
}
未捕获的错误:
Remove-GenericCredential : Credential https://[email protected] /cdn-cgi/l/email-protection not found
At C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\Remove-GenericCredential.Tests.ps1:30 char:76
+ ... xist." { { (Remove-GenericCredential -InternetOrNetworkAddress $Inter ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Remove-GenericCredential
[-] This Command threw an error. The credential does not exist. 44ms
Expected: the expression to throw an exception with message {Remove-GenericCredential : Credential https://[email protected] /cdn-cgi/l/email-protection not found}, an exception was not raised, message was {}
from C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\New-GitHubCredential.Tests.ps1:59 char:176
+ ... e $UserName -Token 'NotAGitHubTokenSpecialCharacters!@#$%^&*') } | sh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at <ScriptBlock>, C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\Remove-GenericCredential.Tests.ps1: line 30
30: It "This Command threw an error. The credential does not exist." { { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false) } | should throw 'Remove-GenericCredential : Credential https://[email protected] /cdn-cgi/l/email-protection not found' }
根据另一个答案,该函数抛出一个非终止错误 https://blogs.technet.microsoft.com/heyscriptingguy/2015/09/16/understanding-non-terminating-errors-in-powershell/因此,它不被认为符合测试Should Throw
,它正在检查终止错误。
有两种方法可以解决这个问题:
- 您可以更改它,以便通过更改您的
Write-Error
to Throw
.
- 您可以更改测试以强制函数抛出终止错误,即使发生非终止错误时也是如此
-ErrorAction Stop
当你调用它时(我可以看到你正在使用-Confirm
,我假设你已经使用过[cmdletbinding()]
在函数中添加常用参数,例如-ErrorAction
).
这是第二个解决方案的示例(我模拟了顶部的函数,以便我可以测试它,但您不需要将其包含在测试脚本中):
Function Remove-GenericCredential {
[cmdletbinding(supportsshouldprocess)]
Param(
$InternetOrNetworkAddress
)
Write-Error "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found"
}
Describe "Remove-GenericCredential Function Tests" {
$InternetOrNetworkAddress = 'https://[email protected] /cdn-cgi/l/email-protection'
Context "Test: Remove-GenericCredential -InternetOrNetworkAddress '$InternetOrNetworkAddress' (Credential does not exist)" {
It "This Command threw an error. The credential does not exist." {
{ (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false -ErrorAction Stop) } | should throw "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found" }
}
}
From help Write-Error
:
Write-Error cmdlet 声明一个非终止错误。默认情况下,
错误通过错误流发送到主机程序
显示,以及输出。
要写入非终止错误,请输入错误消息字符串、ErrorRecord 对象或 Exception 对象。使用 Write-Error 的其他参数来填充错误记录。
非终止错误将错误写入错误流,但它们不会停止命令处理。如果输入项集合中的一项声明非终止错误,则该命令将继续处理集合中的其他项。
要声明终止错误,请使用 Throw 关键字。更多信息请参见about_Throw
(http://go.microsoft.com/fwlink/?LinkID=145153 http://go.microsoft.com/fwlink/?LinkID=145153).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)