Pester不能捕获引发的错误 [英] Pester doesn't catch the thrown error
问题描述
当我运行以下的瘟疫测试时,我希望它能够捕获预期的错误,但是没有.但是,当我使用具有不同throw语句的其他函数运行测试时,它将起作用.
When I run the following pester test I expect it to catch the expected error but it doesn't. But when I run the test with a different function with a different throw statement it works.
Pester测试:
Describe "Remove-GenericCredential Function Tests" {
$InternetOrNetworkAddress = 'https://PesterTestUser@PesterTestURl.com'
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://PesterTestUser@PesterTestURl.com 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://PesterTestUser@PesterTestURl.com 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://PesterTestUser@PesterTestURl.com not found' }
推荐答案
Per the other answer, the function is throwing a non-terminating error and as such its not being considered to match the test of Should Throw
, which is checking for terminating errors.
有两种解决方法:
- 您可以更改它,以便通过将
Write-Error
更改为Throw
来引发终止错误. - 您可以更改测试以强制该函数引发终止错误,即使调用时使用
-ErrorAction Stop
也会发生非终止错误(我可以看到您正在使用-Confirm
,我假设您已经使用过函数中的[cmdletbinding()]
添加常见的参数,例如-ErrorAction
).
- You could change it so that it throws a terminating error by changing your
Write-Error
toThrow
. - You could change the test to force the function to throw a terminating error even when non-terminating ones occur by using
-ErrorAction Stop
when you invoke it (I can see you're using-Confirm
, I assume you have used[cmdletbinding()]
in the function to add the common parameters like-ErrorAction
).
这是第二种解决方案的示例(我在顶部模拟了该函数,以便可以对其进行测试,但是您无需在测试脚本中包括该函数):
Here's an example of the second solution (I've simulated the function at the top so that I could test this, but you don't need to include that in your test script):
Function Remove-GenericCredential {
[cmdletbinding(supportsshouldprocess)]
Param(
$InternetOrNetworkAddress
)
Write-Error "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found"
}
Describe "Remove-GenericCredential Function Tests" {
$InternetOrNetworkAddress = 'https://PesterTestUser@PesterTestURl.com'
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" }
}
}
来自help Write-Error
:
Write-Error cmdlet声明一个非终止错误.默认情况下, 错误在错误流中发送到主机程序 显示,以及输出.
The Write-Error cmdlet declares a non-terminating error. By default, errors are sent in the error stream to the host program to be displayed, along with output.
要写入非终止错误,请输入错误消息字符串,ErrorRecord对象或Exception对象.使用Write-Error的其他参数填充错误记录.
To write a non-terminating error, enter an error message string, an ErrorRecord object, or an Exception object. Use the other parameters of Write-Error to populate the error record.
非终止错误将错误写入错误流,但是它们不会停止命令处理.如果在输入项集合中的一个项上声明了非终止错误,该命令将继续处理该集合中的其他项.
Non-terminating errors write an error to the error stream, but they do not stop command processing. If a non-terminating error is declared on one item in a collection of input items, the command continues to process the other items in the collection.
要声明终止错误,请使用Throw关键字.有关更多信息,请参见about_Throw ( http://go.microsoft.com/fwlink/?LinkID=145153 ).
To declare a terminating error, use the Throw keyword. For more information, see about_Throw (http://go.microsoft.com/fwlink/?LinkID=145153).
这篇关于Pester不能捕获引发的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!