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://[email protected]'
Context "Test: Credential does not exist" {
It "Should have credentials" {
{ (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false) } |
Should Throw "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found"
}
}
}
Error that isn't caught:
Remove-GenericCredential : Credential https://[email protected] 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://[email protected] 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://[email protected] not found' }
Remove-GenericCredential
or at least the code that throws the original error? – Graniah.... | Should -Throw
and not.... | Should Throw
? And, throw should be followed by the string used in your throw statement e.g., { throw "mud" } | Should -Throw "mud" would pass. Otherwise, double check what you think the function is doing, its doing. – Clinician