6

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

未捕获的错误:

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' }
4

2 回答 2

4

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

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

  1. 您可以更改它,以便通过将您更改Write-ErrorThrow.
  2. 您可以更改测试以强制函数抛出终止错误,即使在调用它时使用 using 发生非终止错误-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

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

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

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

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

于 2018-03-26T14:29:54.390 回答
2

这可能是因为 cmdlet 引发了非终止错误,因为 Pester 仅断言终止错误。在这种情况下,需要按如下方式重写测试(根据您的示例使用旧的 Pester v3 语法):

It "Test a non-terminating error gets thrown" {
  $errorThrown = $false;
  try
  {
    Invoke-CmdletThatThrowsNonTerminatingError -ErrorAction Stop
  }
  catch
  {
    $errorThrown = $true
  }
  $errorThrown | Should Be $true
}

在 catch 块中,您还可以获取异常详细信息,例如$_ob上的消息或异常类型

于 2018-03-25T18:18:07.157 回答