This project is read-only.
1
Vote

AssertThrows with an advanced function must use the innerexception message.

description

When i use AssertThrows with an advanced function (ValidateNotNullOrEmpty Attribut), the $MessageExpectedRegExpr parameter must contains the innerexception message.
 

--------------------------------------------------

if (!(test-path variable:_TESTLIB))
{Testlib}

set-strictmode -version 2.0

set-strictmode -version 1.0
 
Function TestAdvFnc{
[CmdletBinding(SupportsShouldProcess=$True)]
param (
     [ValidateNotNullOrEmpty()]
     [Parameter(Position=0)]
    [string[]] $Property) #if $Property is a dynamic parameter, AssertThrows work fine
$i=0
}
 
$Error.Clear()
$sb={TestAdvFnc $null}
 
$Name="Test advanced function"

$msg= $Error[0].ToString() -> $Error[0].Exception.Message

this message is the displayed error message

ParameterBindingValidationException

$msg="Impossible de valider l'argument sur le paramètre « Property »"
 
$sb={TestAdvFnc $null}
AssertThrows System.Management.Automation.ValidationMetadataException $msg $sb $Name
RaiseAssertions

...SHOULDPASS,PASSED,Test advanced function-ExceptionType

...SHOULDPASS,FAILED,Test advanced function-ExceptionMessage,No match for Impossible de valider l'argument sur le paramètre « Property »

 

$smg= $Error[0].Exception.InnerException

this message is not the displayed error message

$msg="L'argument est null ou vide."
 
AssertThrows System.Management.Automation.ValidationMetadataException $msg $sb $Name
RaiseAssertions

...,SHOULDPASS,PASSED,Test advanced function-ExceptionType

...,SHOULDPASS,PASSED,Test advanced function-ExceptionMessage

 
 

---------------------------

Nok, script +AssertThrows +trap

$sb={TestAdvFnc $null}
&{
trap { Write-Host "AssertThrows: $_" -fore green }
if (&$sb) { write-host "ok"}
}
Write-Host "AssertThrows: $($Error[0])rn" -fore White
$Error[0].Exception.Message

not equal to :

$Error[0].Exception.InnerException
 

---------------

ok, cmdlet + AssertThrows +trap

$Error.Clear()
$sb={Select-String -Pattern @($null)}
 
&{
trap { Write-Host "AssertThrows: $_" -fore green }
if (&$sb) { write-host "ok"}
}
Write-Host "AssertThrows: $($Error[0])rn" -fore White
$Error[0].Exception.Message

equal to :

$Error[0].Exception.InnerException
 

----

ok, script + AssertThrows +try-catch

$Error.Clear()
$sb={TestAdvFnc $null}
 
try {
if (&$sb)
{ write-host "ok"}     
} catch {
Write-Host "AssertThrows: $_" -fore green
}

 
Write-Host "AssertThrows: $($Error[0])rn" -fore White
$Error[0].Exception.Message
$Error[0].Exception.InnerException
 

----

ok, trap or try-catch

Function TestAdvFnc{
[CmdletBinding(SupportsShouldProcess=$True)]
param (
     [ValidateNotNullOrEmpty()]
     [Parameter(Position=0)]
    [string[]] $Property)
Throw "Test"
}
 
$Error.Clear()
$sb={TestAdvFnc }
 
&{
trap { Write-Host "AssertThrows: $_" -fore green }
if (&$sb) { write-host "ok"}
}
 
try {
if (&$sb)
{ write-host "ok"}     
} catch {
Write-Host "AssertThrows: $_" -fore green
}

$msg="Test"
AssertThrows System.Management.Automation.RuntimeException $msg $sb
Write-Host "AssertThrows: $($Error[0])rn" -fore White
$Error[0].Exception.Message

undefined

$Error[0].Exception.InnerException

comments