Skip to content

Commit 6e18fd3

Browse files
authored
Unit test catch all blocks (#143)
1 parent aa5b7ed commit 6e18fd3

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Azure Pipelines - Fixes [Issue #130](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/130).
1717
- Azure pipelines - Remove deprecated images, make the build work
1818
- Azure Pipelines - Reverted `PublishCodeCoverageResults` back to v1 to display line level coverage in ADO.
19+
- `Tests` - Added tests for catch blocks in `AdcsCertificationAuthority`, `AcdsOnlineResponder`, `AdcsWebEnrollment`.
1920

2021
## [5.0.0] - 2020-06-20
2122

tests/Unit/DSC_AdcsCertificationAuthority.Tests.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ namespace Microsoft.CertificateServices.Deployment.Common.CA {
220220
-Times 1
221221
}
222222
}
223+
224+
Context 'When there is an unexpected error' {
225+
Mock `
226+
-CommandName Install-AdcsCertificationAuthority `
227+
-MockWith { Throw (New-Object -TypeName 'System.Exception') } `
228+
-Verifiable
229+
230+
It 'Should throw an exception' {
231+
{ Get-TargetResource @testParametersPresent } | Should Throw
232+
}
233+
234+
It 'Should call expected mocks' {
235+
Assert-VerifiableMock
236+
237+
Assert-MockCalled `
238+
-CommandName Install-AdcsCertificationAuthority `
239+
-Exactly `
240+
-Times 1
241+
}
242+
}
223243
}
224244

225245
Describe 'DSC_AdcsCertificationAuthority\Set-TargetResource' {
@@ -409,6 +429,24 @@ namespace Microsoft.CertificateServices.Deployment.Common.CA {
409429
-Times 1
410430
}
411431
}
432+
433+
Context 'Should throw on any other error' {
434+
Mock -CommandName Install-AdcsCertificationAuthority `
435+
-MockWith { Throw (New-Object -TypeName 'System.Exception') } `
436+
-Verifiable
437+
438+
It 'Should throw an exception' {
439+
{ Test-TargetResource @testParametersPresent } | Should Throw
440+
}
441+
442+
It 'Should call expected mocks' {
443+
Assert-VerifiableMock
444+
Assert-MockCalled `
445+
-CommandName Install-AdcsCertificationAuthority `
446+
-Exactly `
447+
-Times 1
448+
}
449+
}
412450
}
413451
}
414452
}

tests/Unit/DSC_AdcsOnlineResponder.Tests.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,25 @@ namespace Microsoft.CertificateServices.Deployment.Common.OCSP {
135135
-Times 1
136136
}
137137
}
138+
139+
Context 'When there is an unexpected error' {
140+
Mock `
141+
-CommandName Install-AdcsOnlineResponder `
142+
-MockWith { Throw (New-Object -TypeName 'System.Exception') } `
143+
-Verifiable
144+
145+
It 'Should throw an exception' {
146+
{ Get-TargetResource @testParametersPresent } | Should Throw
147+
}
148+
149+
It 'Should call expected mocks' {
150+
Assert-VerifiableMock
151+
Assert-MockCalled `
152+
-CommandName Install-AdcsOnlineResponder `
153+
-Exactly `
154+
-Times 1
155+
}
156+
}
138157
}
139158

140159
Describe 'DSC_AdcsOnlineResponder\Set-TargetResource' {
@@ -288,6 +307,24 @@ namespace Microsoft.CertificateServices.Deployment.Common.OCSP {
288307
}
289308
}
290309
}
310+
311+
Context 'Should throw on any other error' {
312+
Mock -CommandName Install-AdcsOnlineResponder `
313+
-MockWith { Throw (New-Object -TypeName 'System.Exception') } `
314+
-Verifiable
315+
316+
It 'Should throw an exception' {
317+
{ Test-TargetResource @testParametersPresent } | Should Throw
318+
}
319+
320+
It 'Should call expected mocks' {
321+
Assert-VerifiableMock
322+
Assert-MockCalled `
323+
-CommandName Install-AdcsOnlineResponder `
324+
-Exactly `
325+
-Times 1
326+
}
327+
}
291328
}
292329
}
293330
}

tests/Unit/DSC_AdcsWebEnrollment.Tests.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ namespace Microsoft.CertificateServices.Deployment.Common.WEP {
142142
-Times 1
143143
}
144144
}
145+
146+
Context 'When there is an unexpected error' {
147+
Mock `
148+
-CommandName Install-AdcsWebEnrollment `
149+
-MockWith { Throw (New-Object -TypeName 'System.Exception') } `
150+
-Verifiable
151+
152+
It 'Should throw an exception' {
153+
{ Get-TargetResource @testParametersPresent } | Should Throw
154+
}
155+
156+
It 'Should call expected mocks' {
157+
Assert-VerifiableMock
158+
159+
Assert-MockCalled `
160+
-CommandName Install-AdcsWebEnrollment `
161+
-Exactly `
162+
-Times 1
163+
}
164+
}
145165
}
146166

147167
Describe 'DSC_AdcsWebEnrollment\Set-TargetResource' {
@@ -299,6 +319,26 @@ namespace Microsoft.CertificateServices.Deployment.Common.WEP {
299319
}
300320
}
301321
}
322+
323+
Context 'Should throw on any other error' {
324+
Mock `
325+
-CommandName Install-AdcsWebEnrollment `
326+
-MockWith { Throw (New-Object -TypeName 'System.Exception') } `
327+
-Verifiable
328+
329+
It 'Should throw an exception' {
330+
{ Test-TargetResource @testParametersPresent } | Should Throw
331+
}
332+
333+
It 'Should call expected mocks' {
334+
Assert-VerifiableMock
335+
336+
Assert-MockCalled `
337+
-CommandName Install-AdcsWebEnrollment `
338+
-Exactly `
339+
-Times 1
340+
}
341+
}
302342
}
303343
}
304344
}

0 commit comments

Comments
 (0)