Skip to content

Commit

Permalink
Unit test catch all blocks (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-hughes authored May 15, 2024
1 parent aa5b7ed commit 6e18fd3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/DSC_AdcsCertificationAuthority.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -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
}
}
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/DSC_AdcsOnlineResponder.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -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
}
}
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/DSC_AdcsWebEnrollment.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -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
}
}
}
}
}
Expand Down

0 comments on commit 6e18fd3

Please sign in to comment.