Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include expected type in exceptions when type is not resolvable #2271

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -177,9 +177,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 @@ -271,6 +271,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