diff --git a/src/functions/assertions/PesterThrow.ps1 b/src/functions/assertions/PesterThrow.ps1 index 311b086e0..0fc126b64 100644 --- a/src/functions/assertions/PesterThrow.ps1 +++ b/src/functions/assertions/PesterThrow.ps1 @@ -70,7 +70,9 @@ # this is for Should -Not -Throw. Once *any* exception was thrown we should fail the assertion # there is no point in filtering the exception, because there should be none $succeeded = -not $actualExceptionWasThrown - if ($true -eq $succeeded) { return [Pester.ShouldResult]@{Succeeded = $succeeded } } + if ($true -eq $succeeded) { + return [Pester.ShouldResult]@{Succeeded = $succeeded } + } $failureMessage = "Expected no exception to be thrown,$(Format-Because $Because) but an exception `"$actualExceptionMessage`" was thrown $actualExceptionLine." return [Pester.ShouldResult] @{ @@ -96,7 +98,7 @@ $filterOnMessage = -not [string]::IsNullOrWhitespace($ExpectedMessage) if ($filterOnMessage) { - $filters += "message like $(Format-Nicely $ExpectedMessage)" + $filters += "message like $(Format-Nicely [System.Management.Automation.WildcardPattern]::Unescape($ExpectedMessage))" if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage))) { $buts += "the message was $(Format-Nicely $actualExceptionMessage)" } @@ -120,7 +122,12 @@ $failureMessage = "Expected an exception$(if($filter) { " with $filter" }) to be thrown,$(Format-Because $Because) but $but. $actualExceptionLine".Trim() $ActualValue = $actualExceptionMessage - $ExpectedValue = if ($filterOnExceptionType) { "type $(Format-Nicely $ExceptionType)" } else { 'any exception' } + $ExpectedValue = if ($filterOnExceptionType) { + "type $(Format-Nicely $ExceptionType)" + } + else { + 'any exception' + } return [Pester.ShouldResult] @{ Succeeded = $false diff --git a/tst/functions/assertions/PesterThrow.Tests.ps1 b/tst/functions/assertions/PesterThrow.Tests.ps1 index 0b15e28fd..8fc7e58e9 100644 --- a/tst/functions/assertions/PesterThrow.Tests.ps1 +++ b/tst/functions/assertions/PesterThrow.Tests.ps1 @@ -164,6 +164,11 @@ InPesterModuleScope { $err.Exception.Message | Verify-Equal "Expected an exception with FullyQualifiedErrorId 'id' to be thrown, but no exception was thrown." } + It "returns the correct assertion message when message filter is used and contain escaped wildcard character" { + $err = { { throw [ArgumentException]"[!]" } | Should -Throw -ExpectedMessage '`[`]' } | Verify-AssertionFailed + $err.Exception.Message | Verify-Equal "Expected an exception with message like '[]' to be thrown, but the message was '[!]'." + } + It 'returns the correct assertion message when exceptions messages differ' { $testDrive = (Get-PSDrive TestDrive).Root $testScriptPath = Join-Path $testDrive test.ps1