Pester doesn't catch the thrown error

痴心易碎 提交于 2020-05-15 04:03:49

问题


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 Test:

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

Error that isn't caught:

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

回答1:


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.

There are two ways you could address this:

  1. You could change it so that it throws a terminating error by changing your Write-Error to Throw.
  2. 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" }
    }
}

From 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).




回答2:


This is probably because the cmdlet is throwing a non-terminating error because Pester only asserts against terminating errors. In this case need to rewrite the test as follows (using old Pester v3 synatax as per your example):

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

In the catch block you can also get exception details such as message or exception type on the $_ ob



来源:https://stackoverflow.com/questions/49181479/pester-doesnt-catch-the-thrown-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!