- This topic has 1 reply, 2 voices, and was last updated 1 month, 3 weeks ago by
Senior Moderator.
-
AuthorPosts
-
-
November 23, 2020 at 2:17 pm #273556
Hi All, I’m running into this issue where I’m casting a variable to [int] type, but unfortunately for me the data being fed is not always in a correct format to succeed 100%. I’ve created a basic test to illustrate my issue:
PowerShell123456789101112131415161718192021Describe "Test Non-Terminating Error" {Context "Non-Terminating Error" {It "Non-Terminating Error" {[int]$Number = 'oc'$Test = 'Test'$obj = [PSCustomObject]@{Number = $NumberTest = $test}$obj.Number | Should -BeNullOrEmpty$obj.Test | Should -Be "Test"}}}When I run this test it always Fails and never processes any of the Assertions:
[-] Test Non-Terminating Error.Non-Terminating Error.Non-Terminating Error 21ms (20ms|1ms) FormatException: Input string was not in a correct format. PSInvalidCastException: Cannot convert value “oc” to type “System.Int32”. Error: “Input string was not in a correct format.” ArgumentTransformationMetadataException: Cannot convert value “oc” to type “System.Int32”. Error: “Input string was not in a correct format.” at , .\assets\_Test.Tests.ps1:11 Tests completed in 1.47s Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0
I’ve already tried:
- Configuring Pester Should ErrorPreference to ‘Continue’
- Setting PowerShell Error Action Preference to ‘SilentlyContinue’ ( $ErrorActionPreference = 'SilentlyContinue')
- Try/Catch doesn’t catch because its Non-Terminating
Is there a way to have pester ignore any non-terminating error that happens within the IT block and just execute the Assertions?
I’m running Pester 5.0.3 on PowerShell 5.1
Please Help!
-
This topic was modified 1 month, 3 weeks ago by
Michael G. Mayoral.
-
November 25, 2020 at 4:09 am #273981
Here the error will occur during the type conversion at [int]$Number = 'oc' . So you having a test for this line is better.
Its best to use [int]::TryParse() method which will not throw any error.
-
-
AuthorPosts
- You must be logged in to reply to this topic.