Pester 没有捕捉到抛出的错误

Kei*_*ith 6 powershell pester

当我运行以下纠缠测试时,我希望它能够捕获预期的错误,但它没有。但是,当我使用不同的 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" }
  }
}
Run Code Online (Sandbox Code Playgroud)

未捕获的错误:

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' }
Run Code Online (Sandbox Code Playgroud)

Mar*_*agg 6

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

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

  1. 您可以更改它,以便通过将您的Write-Errorto更改为引发终止错误Throw
  2. -ErrorAction Stop您可以更改测试以强制函数抛出终止错误,即使在调用它时使用 using 发生非终止错误(我可以看到您正在使用-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" }
    }
}
Run Code Online (Sandbox Code Playgroud)

help Write-Error

Write-Error cmdlet 声明一个非终止错误。默认情况下,错误以错误流的形式发送到主机程序以与输出一起显示。

要写入非终止错误,请输入错误消息字符串、ErrorRecord 对象或 Exception 对象。使用 Write-Error 的其他参数来填充错误记录。

非终止错误将错误写入错误流,但它们不会停止命令处理。如果输入项集合中的一项声明非终止错误,则该命令将继续处理集合中的其他项。

要声明终止错误,请使用 Throw 关键字。有关详细信息,请参阅 about_Throw ( http://go.microsoft.com/fwlink/?LinkID=145153 )。