Skip to content

Commit

Permalink
Include expected type in exceptions when type is not resolvable (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Mar 31, 2023
1 parent a8ad480 commit b36eb51
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/functions/assertions/BeOfType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ function Should-BeOfType($ActualValue, $ExpectedType, [switch] $Negate, [string]
#>
if ($ExpectedType -is [string]) {
# parses type that is provided as a string in brackets (such as [int])
$parsedType = ($ExpectedType -replace '^\[(.*)\]$', '$1') -as [Type]
$trimmedType = $ExpectedType -replace '^\[(.*)\]$', '$1'
$parsedType = $trimmedType -as [Type]
if ($null -eq $parsedType) {
throw [ArgumentException]"Could not find type [$ParsedType]. Make sure that the assembly that contains that type is loaded."
throw [ArgumentException]"Could not find type [$trimmedType]. Make sure that the assembly that contains that type is loaded."
}

$ExpectedType = $parsedType
Expand Down
5 changes: 3 additions & 2 deletions src/functions/assertions/HaveParameter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@

if ($Type -is [string]) {
# parses type that is provided as a string in brackets (such as [int])
$parsedType = ($Type -replace '^\[(.*)\]$', '$1') -as [Type]
$trimmedType = $Type -replace '^\[(.*)\]$', '$1'
$parsedType = $trimmedType -as [Type]
if ($null -eq $parsedType) {
throw [ArgumentException]"Could not find type [$ParsedType]. Make sure that the assembly that contains that type is loaded."
throw [ArgumentException]"Could not find type [$trimmedType]. Make sure that the assembly that contains that type is loaded."
}

$Type = $parsedType
Expand Down
9 changes: 4 additions & 5 deletions tst/functions/assertions/BeOfType.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ InPesterModuleScope {
2.0 | Should -Not -BeOfType ([string])
}

It "throws argument execption if type isn't a loaded type" {
$err = { 5 | Should -Not -BeOfType 'UnknownType' } | Verify-Throw
$err.Exception | Verify-Type ([ArgumentException])
}

It "throws argument execption if type isn't a loaded type" {
$err = { 5 | Should -BeOfType 'UnknownType' } | Verify-Throw
$err.Exception | Verify-Type ([ArgumentException])
# Verify expected type is included in error message
$err.Exception.Message | Verify-Equal 'Could not find type [UnknownType]. Make sure that the assembly that contains that type is loaded.'
}

It "returns the correct assertion message when actual value has a real type" {
Expand All @@ -38,6 +35,8 @@ InPesterModuleScope {
It "throws argument execption if type isn't a loaded type" {
$err = { 5 | Should -Not -BeOfType 'UnknownType' } | Verify-Throw
$err.Exception | Verify-Type ([ArgumentException])
# Verify expected type is included in error message
$err.Exception.Message | Verify-Equal 'Could not find type [UnknownType]. Make sure that the assembly that contains that type is loaded.'
}

It "returns the correct assertion message when actual value has a real type" {
Expand Down
7 changes: 7 additions & 0 deletions tst/functions/assertions/HaveParameter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ InPesterModuleScope {
$err.Exception.Message | Verify-Equal "Expected command Invoke-DummyFunction to have a parameter MandatoryParam, with aliases 'Second' and 'Third', but it didn't have the aliases 'Second' and 'Third'."
}

It "throws ArgumentException when expected type isn't a loaded type" {
$err = { Get-Command 'Invoke-DummyFunction' | Should -HaveParameter MandatoryParam -Type UnknownType } | Verify-Throw
$err.Exception | Verify-Type ([ArgumentException])
# Verify expected type is included in error message
$err.Exception.Message | Verify-Equal 'Could not find type [UnknownType]. Make sure that the assembly that contains that type is loaded.'
}

if ($PSVersionTable.PSVersion.Major -ge 5) {
It "fails if the parameter <ParameterName> does not exist, is not of type <ExpectedType>, has a default value other than '<ExpectedValue>' or has not an ArgumentCompleter" -TestCases @(
@{ParameterName = "MandatoryParam"; ExpectedType = [Object]; ExpectedValue = "" }
Expand Down

0 comments on commit b36eb51

Please sign in to comment.