From f06681b4c98cc0f93871d6ce98239bf38599c318 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 23 Jan 2024 10:39:54 +0100 Subject: [PATCH] Add `New-Exception`, and `PassThru` parameter to exception commands (#115) --- CHANGELOG.md | 25 ++++ ...xception.ps1 => New-ArgumentException.ps1} | 35 +++++- source/Public/New-Exception.ps1 | 69 +++++++++++ source/Public/New-InvalidDataException.ps1 | 15 ++- .../Public/New-InvalidOperationException.ps1 | 39 +++++-- source/Public/New-InvalidResultException.ps1 | 11 +- source/Public/New-NotImplementedException.ps1 | 40 +++++-- source/Public/New-ObjectNotFoundException.ps1 | 13 +-- source/WikiSource/Home.md | 2 - .../Public/New-ArgumentException.Tests.ps1 | 109 ++++++++++++++++++ ...tion.Tests.ps1 => New-Exception.Tests.ps1} | 34 ++++-- .../New-InvalidOperationException.Tests.ps1 | 27 +++++ .../New-NotImplementedException.Tests.ps1 | 17 +++ 13 files changed, 373 insertions(+), 63 deletions(-) rename source/Public/{New-InvalidArgumentException.ps1 => New-ArgumentException.ps1} (54%) create mode 100644 source/Public/New-Exception.ps1 create mode 100644 tests/Unit/Public/New-ArgumentException.Tests.ps1 rename tests/Unit/Public/{New-InvalidArgumentException.Tests.ps1 => New-Exception.Tests.ps1} (60%) diff --git a/CHANGELOG.md b/CHANGELOG.md index e58db14..7dca8a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 that can copy from omne target to the other ([issue #102](https://github.com/dsccommunity/DscResource.Common/issues/102)). - A new parameter `PassThru` that, if specified, returns the path that was set. +- `New-Exception` + - New command that creates and returns an `[System.Exception]`. +- `New-ArgumentException` + - Now takes a parameter `PassThru` that returns the error record that was + created (and does not throw). +- `New-InvalidOperationException` + - Now takes a parameter `PassThru` that returns the error record that was + created (and does not throw) ([issue #98](https://github.com/dsccommunity/DscResource.Common/issues/98)). +- `New-InvalidResultException` + - Now takes a parameter `PassThru` that returns the error record that was + created (and does not throw). +- `New-NotImplementedException` + - Now takes a parameter `PassThru` that returns the error record that was + created (and does not throw). ### Changed @@ -23,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Change the word cmdlet to command throughout in the documentation, code and localization strings. - A meta task now removes the built module from the session if it is imported. +- Wiki source file HOME was modified to not link to README for help after + command documentation now is in the wiki. - `Get-LocalizedData` - Refactored to simplify execution and debugging. The command previously used a steppable pipeline (proxies `Import-LocalizedData`), that was @@ -30,6 +46,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 It just made it more complex and harder to debug. There are more debug messages added to hopefully simplify solving some hard to find edge cases bugs. +- `New-ArgumentException` + - Now has a command alias `New-InvalidArgumentException` and the command + was renamed to match the exception name. +- `New-InvalidDataException` + - The parameter `Message` has a parameter alias `ErrorMessage` to make + the command have the same parameter names as the other `New-*Exception` + commands. ### Fixed @@ -37,6 +60,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed example in documentation that were referencing an invalid command name. - `Get-LocalizedData` - One debug message was wrongly using a format operator ([issue #111](https://github.com/dsccommunity/DscResource.Common/issues/111). +- `New-ObjectNotFoundException` + - Updated typo in comment-based help. ## [0.16.0] - 2023-04-10 diff --git a/source/Public/New-InvalidArgumentException.ps1 b/source/Public/New-ArgumentException.ps1 similarity index 54% rename from source/Public/New-InvalidArgumentException.ps1 rename to source/Public/New-ArgumentException.ps1 index 75d507e..300f8d9 100644 --- a/source/Public/New-InvalidArgumentException.ps1 +++ b/source/Public/New-ArgumentException.ps1 @@ -1,9 +1,9 @@ <# .SYNOPSIS - Creates and throws an invalid argument exception. + Creates and throws or returns an invalid argument exception. .DESCRIPTION - Creates and throws an invalid argument exception. + Creates and throws or returns an invalid argument exception. .PARAMETER Message The message explaining why this error is being thrown. @@ -11,19 +11,31 @@ .PARAMETER ArgumentName The name of the invalid argument that is causing this error to be thrown. + .PARAMETER PassThru + If specified, returns the error record instead of throwing it. + .OUTPUTS None + System.Management.Automation.ErrorRecord .EXAMPLE - New-InvalidArgumentException -ArgumentName 'Action' -Message 'My error message' + New-ArgumentException -ArgumentName 'Action' -Message 'My error message' Creates and throws an invalid argument exception for (parameter) 'Action' with the message 'My error message'. + + .EXAMPLE + $errorRecord = New-ArgumentException -ArgumentName 'Action' -Message 'My error message' -PassThru + + Creates an invalid argument exception for (parameter) 'Action' + with the message 'My error message' and returns the exception. #> -function New-InvalidArgumentException + +function New-ArgumentException { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] [CmdletBinding()] + [Alias('New-InvalidArgumentException')] param ( [Parameter(Mandatory = $true)] @@ -34,7 +46,11 @@ function New-InvalidArgumentException [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] - $ArgumentName + $ArgumentName, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $PassThru ) $argumentException = New-Object -TypeName 'ArgumentException' ` @@ -47,5 +63,12 @@ function New-InvalidArgumentException $errorRecord = New-Object @newObjectParameters - throw $errorRecord + if ($PassThru.IsPresent) + { + return $argumentException + } + else + { + throw $errorRecord + } } diff --git a/source/Public/New-Exception.ps1 b/source/Public/New-Exception.ps1 new file mode 100644 index 0000000..1bb5391 --- /dev/null +++ b/source/Public/New-Exception.ps1 @@ -0,0 +1,69 @@ +<# + .SYNOPSIS + Creates and returns an exception. + + .DESCRIPTION + Creates an exception that will be returned. + + .OUTPUTS + None + + .PARAMETER Message + The message explaining why this error is being thrown. + + .PARAMETER ErrorRecord + The error record containing the exception that is causing this terminating error. + + .OUTPUTS + System.Exception + + .EXAMPLE + $errorRecord = New-Exception -Message 'An error occurred' + + Creates and returns an exception with the message 'An error occurred'. + + .EXAMPLE + try + { + Get-ChildItem -Path $path -ErrorAction 'Stop' + } + catch + { + $exception = New-Exception -Message 'Could not get files' -ErrorRecord $_ + } + + Returns an exception with the message 'Could not get files' and includes + the exception that caused this terminating error. + +#> +function New-Exception +{ + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] + [CmdletBinding()] + [OutputType([System.Exception])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $Message, + + [Parameter()] + [ValidateNotNull()] + [System.Management.Automation.ErrorRecord] + $ErrorRecord + ) + + if ($null -eq $ErrorRecord) + { + $exception = New-Object -TypeName 'System.Exception' ` + -ArgumentList @($Message) + } + else + { + $exception = New-Object -TypeName 'System.Exception' ` + -ArgumentList @($Message, $ErrorRecord.Exception) + } + + return $exception +} diff --git a/source/Public/New-InvalidDataException.ps1 b/source/Public/New-InvalidDataException.ps1 index 35e31d5..11e45f7 100644 --- a/source/Public/New-InvalidDataException.ps1 +++ b/source/Public/New-InvalidDataException.ps1 @@ -11,11 +11,11 @@ .PARAMETER ErrorId The error Id to assign to the exception. - .PARAMETER ErrorMessage + .PARAMETER Message The error message to assign to the exception. .EXAMPLE - New-InvalidDataException -ErrorId 'InvalidDataError' -ErrorMessage 'My error message' + New-InvalidDataException -ErrorId 'InvalidDataError' -Message 'My error message' Creates and throws an invalid data exception with the error id 'InvalidDataError' and with the message 'My error message'. @@ -31,16 +31,19 @@ function New-InvalidDataException $ErrorId, [Parameter(Mandatory = $true)] + [Alias('ErrorMessage')] [System.String] - $ErrorMessage + $Message ) $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidData + $exception = New-Object ` - -TypeName System.InvalidOperationException ` - -ArgumentList $ErrorMessage + -TypeName 'System.InvalidOperationException' ` + -ArgumentList $Message + $errorRecord = New-Object ` - -TypeName System.Management.Automation.ErrorRecord ` + -TypeName 'System.Management.Automation.ErrorRecord' ` -ArgumentList $exception, $ErrorId, $errorCategory, $null throw $errorRecord diff --git a/source/Public/New-InvalidOperationException.ps1 b/source/Public/New-InvalidOperationException.ps1 index 90ba5f8..28f4ce5 100644 --- a/source/Public/New-InvalidOperationException.ps1 +++ b/source/Public/New-InvalidOperationException.ps1 @@ -1,12 +1,13 @@ <# .SYNOPSIS - Creates and throws an invalid operation exception. + Creates and throws or returns an invalid operation exception. .DESCRIPTION - Creates and throws an invalid operation exception. + Creates and throws or returns an invalid operation exception. .OUTPUTS - None + None. If the PassThru parameter is not specified the command throws an error record. + System.InvalidOperationException. If the PassThru parameter is specified the command returns an error record. .PARAMETER Message The message explaining why this error is being thrown. @@ -14,6 +15,9 @@ .PARAMETER ErrorRecord The error record containing the exception that is causing this terminating error. + .PARAMETER PassThru + If specified, returns the error record instead of throwing it. + .EXAMPLE try { @@ -26,11 +30,17 @@ Creates and throws an invalid operation exception with the message 'My error message' and includes the exception that caused this terminating error. -#> + + .EXAMPLE + $errorRecord = New-InvalidOperationException -Message 'My error message' -PassThru + + Creates and returns an invalid operation exception with the message 'My error message'. + #> function New-InvalidOperationException { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] [CmdletBinding()] + [OutputType([System.InvalidOperationException])] param ( [Parameter(Mandatory = $true)] @@ -41,17 +51,21 @@ function New-InvalidOperationException [Parameter()] [ValidateNotNull()] [System.Management.Automation.ErrorRecord] - $ErrorRecord + $ErrorRecord, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $PassThru ) if ($null -eq $ErrorRecord) { - $invalidOperationException = New-Object -TypeName 'InvalidOperationException' ` + $invalidOperationException = New-Object -TypeName 'System.InvalidOperationException' ` -ArgumentList @($Message) } else { - $invalidOperationException = New-Object -TypeName 'InvalidOperationException' ` + $invalidOperationException = New-Object -TypeName 'System.InvalidOperationException' ` -ArgumentList @($Message, $ErrorRecord.Exception) } @@ -65,7 +79,14 @@ function New-InvalidOperationException ) } - $errorRecordToThrow = New-Object @newObjectParameters + $errorRecordToReturn = New-Object @newObjectParameters - throw $errorRecordToThrow + if ($PassThru.IsPresent) + { + return $invalidOperationException + } + else + { + throw $errorRecordToReturn + } } diff --git a/source/Public/New-InvalidResultException.ps1 b/source/Public/New-InvalidResultException.ps1 index 3dec5ed..9332a3e 100644 --- a/source/Public/New-InvalidResultException.ps1 +++ b/source/Public/New-InvalidResultException.ps1 @@ -53,16 +53,7 @@ function New-InvalidResultException $ErrorRecord ) - if ($null -eq $ErrorRecord) - { - $exception = New-Object -TypeName 'System.Exception' ` - -ArgumentList @($Message) - } - else - { - $exception = New-Object -TypeName 'System.Exception' ` - -ArgumentList @($Message, $ErrorRecord.Exception) - } + $exception = New-Exception @PSBoundParameters $newObjectParameters = @{ TypeName = 'System.Management.Automation.ErrorRecord' diff --git a/source/Public/New-NotImplementedException.ps1 b/source/Public/New-NotImplementedException.ps1 index 855819c..525aa65 100644 --- a/source/Public/New-NotImplementedException.ps1 +++ b/source/Public/New-NotImplementedException.ps1 @@ -1,9 +1,9 @@ <# .SYNOPSIS - Creates and throws an not implemented exception. + Creates and throws or returns an not implemented exception. .DESCRIPTION - Creates and throws an not implemented exception. + Creates and throws or returns an not implemented exception. .PARAMETER Message The message explaining why this error is being thrown. @@ -11,8 +11,12 @@ .PARAMETER ErrorRecord The error record containing the exception that is causing this terminating error. + .PARAMETER PassThru + If specified, returns the error record instead of throwing it. + .OUTPUTS None + System.NotImplementedException .EXAMPLE if ($notImplementedFeature) @@ -21,12 +25,19 @@ } Creates and throws an not implemented exception with the message 'This feature - is not implemented yet' + is not implemented yet'. + + .EXAMPLE + $errorRecord = New-NotImplementedException -Message 'This feature is not implemented yet' -PassThru + + Creates and returns an not implemented exception with the message 'This feature + is not implemented yet'. #> function New-NotImplementedException { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] [CmdletBinding()] + [OutputType([System.NotImplementedException])] param ( [Parameter(Mandatory = $true)] @@ -37,31 +48,42 @@ function New-NotImplementedException [Parameter()] [ValidateNotNull()] [System.Management.Automation.ErrorRecord] - $ErrorRecord + $ErrorRecord, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $PassThru ) if ($null -eq $ErrorRecord) { - $invalidOperationException = New-Object -TypeName 'NotImplementedException' ` + $notImplementedException = New-Object -TypeName 'System.NotImplementedException' ` -ArgumentList @($Message) } else { - $invalidOperationException = New-Object -TypeName 'NotImplementedException' ` + $notImplementedException = New-Object -TypeName 'System.NotImplementedException' ` -ArgumentList @($Message, $ErrorRecord.Exception) } $newObjectParameters = @{ TypeName = 'System.Management.Automation.ErrorRecord' ArgumentList = @( - $invalidOperationException.ToString(), + $notImplementedException.ToString(), 'MachineStateIncorrect', 'NotImplemented', $null ) } - $errorRecordToThrow = New-Object @newObjectParameters + $errorRecord = New-Object @newObjectParameters - throw $errorRecordToThrow + if ($PassThru.IsPresent) + { + return $notImplementedException + } + else + { + throw $errorRecord + } } diff --git a/source/Public/New-ObjectNotFoundException.ps1 b/source/Public/New-ObjectNotFoundException.ps1 index 2eec32c..c593f1a 100644 --- a/source/Public/New-ObjectNotFoundException.ps1 +++ b/source/Public/New-ObjectNotFoundException.ps1 @@ -24,7 +24,7 @@ } catch { - New-ObjectNotFoundException -Message 'COuld not get files' -ErrorRecord $_ + New-ObjectNotFoundException -Message 'Could not get files' -ErrorRecord $_ } Creates and throws an object not found exception with the message 'Could not @@ -47,16 +47,7 @@ function New-ObjectNotFoundException $ErrorRecord ) - if ($null -eq $ErrorRecord) - { - $exception = New-Object -TypeName 'System.Exception' ` - -ArgumentList @($Message) - } - else - { - $exception = New-Object -TypeName 'System.Exception' ` - -ArgumentList @($Message, $ErrorRecord.Exception) - } + $exception = New-Exception @PSBoundParameters $newObjectParameters = @{ TypeName = 'System.Management.Automation.ErrorRecord' diff --git a/source/WikiSource/Home.md b/source/WikiSource/Home.md index ee50fe0..30b3184 100644 --- a/source/WikiSource/Home.md +++ b/source/WikiSource/Home.md @@ -2,8 +2,6 @@ *DscResource.Common v#.#.#* -For documentation please see the [README.md](https://github.com/dsccommunity/DscResource.Common/blob/main/README.md) - Please leave comments, feature requests, and bug reports for this module in the [issues section](https://github.com/dsccommunity/DscResource.Common/issues) for this repository. diff --git a/tests/Unit/Public/New-ArgumentException.Tests.ps1 b/tests/Unit/Public/New-ArgumentException.Tests.ps1 new file mode 100644 index 0000000..33db675 --- /dev/null +++ b/tests/Unit/Public/New-ArgumentException.Tests.ps1 @@ -0,0 +1,109 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +param () + +BeforeDiscovery { + try + { + if (-not (Get-Module -Name 'DscResource.Test')) + { + # Assumes dependencies has been resolved, so if this module is not available, run 'noop' task. + if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # If the dependencies has not been resolved, this will throw an error. + Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' + } + } + catch [System.IO.FileNotFoundException] + { + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' + } +} + +BeforeAll { + $script:moduleName = 'DscResource.Common' + + # Make sure there are not other modules imported that will conflict with mocks. + Get-Module -Name $script:moduleName -All | Remove-Module -Force + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + Remove-Module -Name $script:moduleName +} + +Describe 'New-ArgumentException' { + Context 'When calling with both the Message and ArgumentName parameter' { + It 'Should throw the correct error' { + $mockErrorMessage = 'Mocked error' + $mockArgumentName = 'MockArgument' + + # Wildcard processing needed to handle differing Powershell 5/6/7 exception output + { New-ArgumentException -Message $mockErrorMessage -ArgumentName $mockArgumentName } | + Should -Throw -PassThru | Select-Object -ExpandProperty Exception | + Should -BeLike ('{0}*Parameter*{1}*' -f $mockErrorMesssage, $mockArgumentName) + } + } + + Context 'When using command alias New-InvalidArgumentException' { + It 'Should throw the correct error' { + $mockErrorMessage = 'Mocked error' + $mockArgumentName = 'MockArgument' + + # Wildcard processing needed to handle differing Powershell 5/6/7 exception output + { New-InvalidArgumentException -Message $mockErrorMessage -ArgumentName $mockArgumentName } | + Should -Throw -PassThru | Select-Object -ExpandProperty Exception | + Should -BeLike ('{0}*Parameter*{1}*' -f $mockErrorMesssage, $mockArgumentName) + } + } + + Context 'When calling with the PassThru parameter' { + It 'Should return the correct error record' { + $mockErrorMessage = 'Mocked error' + $mockArgumentName = 'MockArgument' + + $result = New-ArgumentException -Message $mockErrorMessage -ArgumentName $mockArgumentName -PassThru + $result | Should -BeOfType 'System.ArgumentException' + <# + There is a difference between how Windows PowerShell and PowerShell + outputs this error message. The regular expression handles both cases. + + Windows PowerShell message: + Mocked error + Parameter name: MockArgument + + PowerShell message: + Mocked error (Parameter 'MockArgument') + #> + $result.Message | Should -Match ("{0}\r?\n?.*\(?Parameter (?:name: )?'?{1}'?\)?" -f $mockErrorMessage, $mockArgumentName) + $result.ParamName | Should -Be $mockArgumentName + } + } + + Context 'When calling without the PassThru parameter' { + It 'Should throw the correct error' { + $mockErrorMessage = 'Mocked error' + $mockArgumentName = 'MockArgument' + + $result = { New-ArgumentException -Message $mockErrorMessage -ArgumentName $mockArgumentName } | + Should -Throw -PassThru + + $result | Should -BeOfType 'System.Management.Automation.ErrorRecord' + $result | Select-Object -ExpandProperty 'Exception' | + Should -BeLike ('System.ArgumentException: {0}*Parameter*{1}*' -f $mockErrorMessage, $mockArgumentName) + } + } +} diff --git a/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 b/tests/Unit/Public/New-Exception.Tests.ps1 similarity index 60% rename from tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 rename to tests/Unit/Public/New-Exception.Tests.ps1 index 55d6451..d9fe35f 100644 --- a/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 +++ b/tests/Unit/Public/New-Exception.Tests.ps1 @@ -45,16 +45,30 @@ AfterAll { Remove-Module -Name $script:moduleName } -Describe 'New-InvalidArgumentException' { - Context 'When calling with both the Message and ArgumentName parameter' { - It 'Should throw the correct error' { - $mockErrorMessage = 'Mocked error' - $mockArgumentName = 'MockArgument' - - # Wildcard processing needed to handle differing Powershell 5/6/7 exception output - { New-InvalidArgumentException -Message $mockErrorMessage -ArgumentName $mockArgumentName } | - Should -Throw -PassThru | Select-Object -ExpandProperty Exception | - Should -BeLike ('{0}*Parameter*{1}*' -f $mockErrorMesssage, $mockArgumentName) +Describe 'New-Exception' { + Context 'When calling with Message parameter only' { + It 'Should return the correct exception' { + $mockMessage = 'Test exception message' + $result = New-Exception -Message $mockMessage + $result | Should -BeOfType System.Exception + $result.Message | Should -Be $mockMessage + } + } + + Context 'When calling with both the Message and ErrorRecord parameter' { + It 'Should return the correct exception' { + $mockMessage = 'Test exception message' + $mockInnerExceptionMessage = 'Test inner exception message' + + $mockInnerException = New-Object -TypeName System.Exception -ArgumentList $mockInnerExceptionMessage + $mockErrorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord ` + -ArgumentList $mockInnerException, $null, 'NotSpecified', $null + + $result = New-Exception -Message $mockMessage -ErrorRecord $mockErrorRecord + + $result | Should -BeOfType System.Exception + $result.Message | Should -Be $mockMessage + $result.InnerException.Message | Should -Be $mockInnerExceptionMessage } } } diff --git a/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 b/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 index 100453c..6344cc7 100644 --- a/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 @@ -71,4 +71,31 @@ Describe 'New-InvalidOperationException' { $mockErrorMessage, $mockExceptionErrorMessage) } } + + Context 'When calling with Message and PassThru parameters' { + It 'Should return the correct error record' { + $mockErrorMessage = 'Mocked error' + + $result = New-InvalidOperationException -Message $mockErrorMessage -PassThru + + $result | Should -BeOfType 'System.InvalidOperationException' + $result.Message | Should -Be $mockErrorMessage + } + } + + Context 'When calling with Message, ErrorRecord and PassThru parameters' { + It 'Should return the correct error record' { + $mockErrorMessage = 'Mocked error' + $mockExceptionErrorMessage = 'Mocked exception error message' + + $mockException = New-Object -TypeName 'System.Exception' -ArgumentList $mockExceptionErrorMessage + $mockErrorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' ` + -ArgumentList $mockException, $null, 'InvalidResult', $null + + $result = New-InvalidOperationException -Message $mockErrorMessage -ErrorRecord $mockErrorRecord -PassThru + + $result | Should -BeOfType 'System.InvalidOperationException' + $result.Message | Should -Be $mockErrorMessage + } + } } diff --git a/tests/Unit/Public/New-NotImplementedException.Tests.ps1 b/tests/Unit/Public/New-NotImplementedException.Tests.ps1 index 57fdf15..74aae44 100644 --- a/tests/Unit/Public/New-NotImplementedException.Tests.ps1 +++ b/tests/Unit/Public/New-NotImplementedException.Tests.ps1 @@ -71,4 +71,21 @@ Describe 'New-NotImplementedException' { $mockErrorMessage, $mockExceptionErrorMessage) } } + + Context 'When called with PassThru parameter' { + It 'Should return the correct error record' { + $mockErrorMessage = 'Mocked error' + $mockExceptionErrorMessage = 'Mocked exception error message' + + $mockException = New-Object -TypeName System.Exception -ArgumentList $mockExceptionErrorMessage + $mockErrorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord ` + -ArgumentList $mockException, $null, 'InvalidResult', $null + + $result = New-NotImplementedException -Message $mockErrorMessage -ErrorRecord $mockErrorRecord -PassThru + + $result | Should -BeOfType System.NotImplementedException + $result.Message | Should -Be $mockErrorMessage + } + } + }