5
Vote

Expected Exception feature is not handling .NET Exception classes that are declared internal instead of public e.g. Parameter Validation Exceptions

description

Testing the logic in PowerShell Parameter Validation Attributes doesn't work at the moment. The framework depends on the Exception class to be declared public instead of internal.
I reported this issue to the PowerShell group at connect.mircosoft.com
https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=481373
 
Workaround 1: Wrap the parameter validation attribute exceptions into an System.InvalidArgumentException. E.g. declare helper function that does the validation and call this helper function within a try catch block.
There needs to be a feature change to handle exceptions that are declared internal.

comments

Thell wrote Dec 30, 2009 at 8:37 PM

Workaround2:

Alter the main try/catch block exception checking statement to:
  if ( ($ActualExceptionTypeName -eq $ExpectedExceptionTypeName) -or
    ( ($ExpectedExceptionTypeName -eq ($_.Exception.GetType()).UnderlyingSystemType.BaseType.FullName) -and
    ([regex]::IsMatch($CurrentFunction.Definition, "Actual: .*$ActualExceptionTypeName") ) ) )
  {
    Report-TestResult -Test $CurrentFunction -Result "PASS"
  }
  else
  {
    Report-TestResult -Test $CurrentFunction -Result "FAIL" -Reason $_
  }
And when defining the test function use something similar to:
function Test.readFuncGuardHeader_InvalidPath(
[switch] $Category_Exported,
[System.Management.Automation.ParameterBindingException] $ExpectedException `
    = "Actual: System.Management.Automation.ParameterBindingValidationException") {
#Arrange

#Act
$Actual = Get-FuncGuard -Path $TMP_TESTFILES_PATH\invalid\

#Assert
}

That's my 'real-world' usage of it.

wrote Dec 30, 2009 at 8:37 PM

wrote Jun 22, 2012 at 11:54 PM

wrote Feb 14, 2013 at 1:40 AM

wrote Mar 30, 2013 at 12:15 PM

wrote Jun 20, 2013 at 3:08 PM