gpt4 book ai didi

powershell - Pester 没有捕捉到抛出的错误

转载 作者:行者123 更新时间:2023-12-01 00:20:22 27 4
gpt4 key购买 nike

当我运行以下纠缠测试时,我希望它能够捕获预期的错误,但它没有。但是,当我使用不同的 throw 语句使用不同的函数运行测试时,它会起作用。

纠缠测试:

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" }
}
}

Uncaught Error :
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' }

最佳答案

根据另一个答案,该函数正在抛出 non-terminating error因此它不被认为与 Should Throw 的测试相匹配,这是检查终止错误。

有两种方法可以解决这个问题:

  • 您可以更改它,以便通过更改您的 Write-Error 来引发终止错误。至 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://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 :

    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.

    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.

    To declare a terminating error, use the Throw keyword. For more information, see about_Throw (http://go.microsoft.com/fwlink/?LinkID=145153).

    关于powershell - Pester 没有捕捉到抛出的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49181479/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com