From 6e18fd301de7e621bd1ec8c77de9e091ffe2b7de Mon Sep 17 00:00:00 2001 From: Daniel Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Wed, 15 May 2024 18:16:55 +0100 Subject: [PATCH] Unit test catch all blocks (#143) --- CHANGELOG.md | 1 + .../DSC_AdcsCertificationAuthority.Tests.ps1 | 38 ++++++++++++++++++ tests/Unit/DSC_AdcsOnlineResponder.Tests.ps1 | 37 +++++++++++++++++ tests/Unit/DSC_AdcsWebEnrollment.Tests.ps1 | 40 +++++++++++++++++++ 4 files changed, 116 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e1c75..8d80c82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Azure Pipelines - Fixes [Issue #130](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/130). - Azure pipelines - Remove deprecated images, make the build work - Azure Pipelines - Reverted `PublishCodeCoverageResults` back to v1 to display line level coverage in ADO. +- `Tests` - Added tests for catch blocks in `AdcsCertificationAuthority`, `AcdsOnlineResponder`, `AdcsWebEnrollment`. ## [5.0.0] - 2020-06-20 diff --git a/tests/Unit/DSC_AdcsCertificationAuthority.Tests.ps1 b/tests/Unit/DSC_AdcsCertificationAuthority.Tests.ps1 index 2aa7612..b95c793 100644 --- a/tests/Unit/DSC_AdcsCertificationAuthority.Tests.ps1 +++ b/tests/Unit/DSC_AdcsCertificationAuthority.Tests.ps1 @@ -220,6 +220,26 @@ namespace Microsoft.CertificateServices.Deployment.Common.CA { -Times 1 } } + + Context 'When there is an unexpected error' { + Mock ` + -CommandName Install-AdcsCertificationAuthority ` + -MockWith { Throw (New-Object -TypeName 'System.Exception') } ` + -Verifiable + + It 'Should throw an exception' { + { Get-TargetResource @testParametersPresent } | Should Throw + } + + It 'Should call expected mocks' { + Assert-VerifiableMock + + Assert-MockCalled ` + -CommandName Install-AdcsCertificationAuthority ` + -Exactly ` + -Times 1 + } + } } Describe 'DSC_AdcsCertificationAuthority\Set-TargetResource' { @@ -409,6 +429,24 @@ namespace Microsoft.CertificateServices.Deployment.Common.CA { -Times 1 } } + + Context 'Should throw on any other error' { + Mock -CommandName Install-AdcsCertificationAuthority ` + -MockWith { Throw (New-Object -TypeName 'System.Exception') } ` + -Verifiable + + It 'Should throw an exception' { + { Test-TargetResource @testParametersPresent } | Should Throw + } + + It 'Should call expected mocks' { + Assert-VerifiableMock + Assert-MockCalled ` + -CommandName Install-AdcsCertificationAuthority ` + -Exactly ` + -Times 1 + } + } } } } diff --git a/tests/Unit/DSC_AdcsOnlineResponder.Tests.ps1 b/tests/Unit/DSC_AdcsOnlineResponder.Tests.ps1 index 77f3385..809c6ef 100644 --- a/tests/Unit/DSC_AdcsOnlineResponder.Tests.ps1 +++ b/tests/Unit/DSC_AdcsOnlineResponder.Tests.ps1 @@ -135,6 +135,25 @@ namespace Microsoft.CertificateServices.Deployment.Common.OCSP { -Times 1 } } + + Context 'When there is an unexpected error' { + Mock ` + -CommandName Install-AdcsOnlineResponder ` + -MockWith { Throw (New-Object -TypeName 'System.Exception') } ` + -Verifiable + + It 'Should throw an exception' { + { Get-TargetResource @testParametersPresent } | Should Throw + } + + It 'Should call expected mocks' { + Assert-VerifiableMock + Assert-MockCalled ` + -CommandName Install-AdcsOnlineResponder ` + -Exactly ` + -Times 1 + } + } } Describe 'DSC_AdcsOnlineResponder\Set-TargetResource' { @@ -288,6 +307,24 @@ namespace Microsoft.CertificateServices.Deployment.Common.OCSP { } } } + + Context 'Should throw on any other error' { + Mock -CommandName Install-AdcsOnlineResponder ` + -MockWith { Throw (New-Object -TypeName 'System.Exception') } ` + -Verifiable + + It 'Should throw an exception' { + { Test-TargetResource @testParametersPresent } | Should Throw + } + + It 'Should call expected mocks' { + Assert-VerifiableMock + Assert-MockCalled ` + -CommandName Install-AdcsOnlineResponder ` + -Exactly ` + -Times 1 + } + } } } } diff --git a/tests/Unit/DSC_AdcsWebEnrollment.Tests.ps1 b/tests/Unit/DSC_AdcsWebEnrollment.Tests.ps1 index 7824da4..b2e3ca5 100644 --- a/tests/Unit/DSC_AdcsWebEnrollment.Tests.ps1 +++ b/tests/Unit/DSC_AdcsWebEnrollment.Tests.ps1 @@ -142,6 +142,26 @@ namespace Microsoft.CertificateServices.Deployment.Common.WEP { -Times 1 } } + + Context 'When there is an unexpected error' { + Mock ` + -CommandName Install-AdcsWebEnrollment ` + -MockWith { Throw (New-Object -TypeName 'System.Exception') } ` + -Verifiable + + It 'Should throw an exception' { + { Get-TargetResource @testParametersPresent } | Should Throw + } + + It 'Should call expected mocks' { + Assert-VerifiableMock + + Assert-MockCalled ` + -CommandName Install-AdcsWebEnrollment ` + -Exactly ` + -Times 1 + } + } } Describe 'DSC_AdcsWebEnrollment\Set-TargetResource' { @@ -299,6 +319,26 @@ namespace Microsoft.CertificateServices.Deployment.Common.WEP { } } } + + Context 'Should throw on any other error' { + Mock ` + -CommandName Install-AdcsWebEnrollment ` + -MockWith { Throw (New-Object -TypeName 'System.Exception') } ` + -Verifiable + + It 'Should throw an exception' { + { Test-TargetResource @testParametersPresent } | Should Throw + } + + It 'Should call expected mocks' { + Assert-VerifiableMock + + Assert-MockCalled ` + -CommandName Install-AdcsWebEnrollment ` + -Exactly ` + -Times 1 + } + } } } }