Skip to content

Commit 83eaab0

Browse files
committed
Finished unit tests
1 parent 6176102 commit 83eaab0

3 files changed

Lines changed: 278 additions & 20 deletions

File tree

Tests/Unit/DSR_IniSettingsFile.Tests.ps1

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,60 @@ try
235235
-Exactly 1
236236
}
237237
}
238+
239+
Context 'File does not exist' {
240+
# verifiable (should be called) mocks
241+
Mock `
242+
-CommandName Assert-ParametersValid `
243+
-ModuleName 'DSR_IniSettingsFile' `
244+
-Verifiable
245+
246+
Mock `
247+
-CommandName Set-IniSettingFileValue `
248+
-ParameterFilter {
249+
($path -eq $script:testTextFile) -and `
250+
($section -eq $script:testSection) -and `
251+
($key -eq $script:testKey) -and `
252+
($value -eq $script:testText)
253+
} `
254+
-Verifiable
255+
256+
Mock `
257+
-CommandName Test-Path `
258+
-ModuleName 'DSR_IniSettingsFile' `
259+
-MockWith { $false } `
260+
-Verifiable
261+
262+
Mock `
263+
-CommandName Out-File `
264+
-ModuleName 'DSR_IniSettingsFile' `
265+
-Verifiable
266+
267+
It 'Should not throw an exception' {
268+
{ Set-TargetResource `
269+
-Path $script:testTextFile `
270+
-Section $script:testSection `
271+
-Key $script:testKey `
272+
-Text $script:testText `
273+
-Verbose
274+
} | Should -Not -Throw
275+
}
276+
277+
It 'Should call the expected mocks' {
278+
Assert-VerifiableMock
279+
Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1
280+
281+
Assert-MockCalled `
282+
-CommandName Set-IniSettingFileValue `
283+
-ParameterFilter {
284+
($path -eq $script:testTextFile) -and `
285+
($section -eq $script:testSection) -and `
286+
($key -eq $script:testKey) -and `
287+
($value -eq $script:testText)
288+
} `
289+
-Exactly 1
290+
}
291+
}
238292
}
239293
#endregion
240294

@@ -433,6 +487,33 @@ try
433487
-Exactly 1
434488
}
435489
}
490+
491+
Context 'File does not exist' {
492+
# verifiable (should be called) mocks
493+
Mock `
494+
-CommandName Assert-ParametersValid `
495+
-ModuleName 'DSR_IniSettingsFile' `
496+
-Verifiable
497+
498+
Mock `
499+
-CommandName Test-Path `
500+
-ModuleName 'DSR_IniSettingsFile' `
501+
-MockWith { $false }
502+
503+
It 'Should return false' {
504+
$result = Test-TargetResource `
505+
-Path $script:testTextFile `
506+
-Section $script:testSection `
507+
-Key $script:testKey `
508+
-Text $script:testText
509+
510+
$result | Should -Be $false
511+
}
512+
513+
It 'Should call the expected mocks' {
514+
Assert-VerifiableMock
515+
}
516+
}
436517
}
437518
#endregion
438519

@@ -441,10 +522,10 @@ try
441522
Context 'File exists' {
442523
# verifiable (should be called) mocks
443524
Mock `
444-
-CommandName Split-Path `
445-
-ParameterFilter { $path -eq $script:testTextFile } `
446-
-MockWith { $script:testTextFile } `
447-
-Verifiable
525+
-CommandName Split-Path `
526+
-ParameterFilter { $path -eq $script:testTextFile } `
527+
-MockWith { $script:testTextFile } `
528+
-Verifiable
448529

449530
Mock `
450531
-CommandName Test-Path `
@@ -470,10 +551,10 @@ try
470551
Context 'File parent does not exist' {
471552
# verifiable (should be called) mocks
472553
Mock `
473-
-CommandName Split-Path `
474-
-ParameterFilter { $path -eq $script:testTextFile } `
475-
-MockWith { $script:testTextFile } `
476-
-Verifiable
554+
-CommandName Split-Path `
555+
-ParameterFilter { $path -eq $script:testTextFile } `
556+
-MockWith { $script:testTextFile } `
557+
-Verifiable
477558

478559
Mock `
479560
-CommandName Test-Path `

Tests/Unit/DSR_KeyValuePairFile.Tests.ps1

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,56 @@ $($script:testAddedName)=$($script:testText)
184184

185185
#region Function Set-TargetResource
186186
Describe 'DSR_KeyValuePairFile\Set-TargetResource' {
187+
Context 'File does not exist' {
188+
# verifiable (should be called) mocks
189+
Mock `
190+
-CommandName Assert-ParametersValid `
191+
-ModuleName 'DSR_KeyValuePairFile' `
192+
-Verifiable
193+
194+
Mock `
195+
-CommandName Get-Content `
196+
-ParameterFilter { $path -eq $script:testTextFile } `
197+
-MockWith { $null } `
198+
-Verifiable
199+
200+
Mock `
201+
-CommandName Set-Content `
202+
-ParameterFilter {
203+
($path -eq $script:testTextFile) -and `
204+
($value -eq "$script:testName=$script:testText")
205+
} `
206+
-Verifiable
207+
208+
It 'Should not throw an exception' {
209+
{ Set-TargetResource `
210+
-Path $script:testTextFile `
211+
-Name $script:testName `
212+
-Ensure 'Present' `
213+
-Text $script:testText `
214+
-Verbose
215+
} | Should -Not -Throw
216+
}
217+
218+
It 'Should call the expected mocks' {
219+
Assert-VerifiableMock
220+
Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1
221+
222+
Assert-MockCalled `
223+
-CommandName Get-Content `
224+
-ParameterFilter { $path -eq $script:testTextFile } `
225+
-Exactly 1
226+
227+
Assert-MockCalled `
228+
-CommandName Set-Content `
229+
-ParameterFilter {
230+
($path -eq $script:testTextFile) -and `
231+
($value -eq "$script:testName=$script:testText")
232+
} `
233+
-Exactly 1
234+
}
235+
}
236+
187237
Context 'File exists and contains matching key that should exist' {
188238
# verifiable (should be called) mocks
189239
Mock `
@@ -488,7 +538,7 @@ $($script:testAddedName)=$($script:testText)
488538
-ModuleName 'DSR_KeyValuePairFile' `
489539
-Verifiable
490540

491-
Mock `
541+
Mock `
492542
-CommandName Test-Path `
493543
-ModuleName 'DSR_KeyValuePairFile' `
494544
-MockWith { $true } `
@@ -533,10 +583,10 @@ $($script:testAddedName)=$($script:testText)
533583
-Verifiable
534584

535585
Mock `
536-
-CommandName Test-Path `
537-
-ModuleName 'DSR_KeyValuePairFile' `
538-
-MockWith { $true } `
539-
-Verifiable
586+
-CommandName Test-Path `
587+
-ModuleName 'DSR_KeyValuePairFile' `
588+
-MockWith { $true } `
589+
-Verifiable
540590

541591
Mock `
542592
-CommandName Get-Content `
@@ -575,7 +625,7 @@ $($script:testAddedName)=$($script:testText)
575625
-ModuleName 'DSR_KeyValuePairFile' `
576626
-Verifiable
577627

578-
Mock `
628+
Mock `
579629
-CommandName Test-Path `
580630
-ModuleName 'DSR_KeyValuePairFile' `
581631
-MockWith { $true } `
@@ -619,7 +669,7 @@ $($script:testAddedName)=$($script:testText)
619669
-ModuleName 'DSR_KeyValuePairFile' `
620670
-Verifiable
621671

622-
Mock `
672+
Mock `
623673
-CommandName Test-Path `
624674
-ModuleName 'DSR_KeyValuePairFile' `
625675
-MockWith { $true } `
@@ -664,7 +714,7 @@ $($script:testAddedName)=$($script:testText)
664714
-ModuleName 'DSR_KeyValuePairFile' `
665715
-Verifiable
666716

667-
Mock `
717+
Mock `
668718
-CommandName Test-Path `
669719
-ModuleName 'DSR_KeyValuePairFile' `
670720
-MockWith { $true } `
@@ -709,7 +759,7 @@ $($script:testAddedName)=$($script:testText)
709759
-ModuleName 'DSR_KeyValuePairFile' `
710760
-Verifiable
711761

712-
Mock `
762+
Mock `
713763
-CommandName Test-Path `
714764
-ModuleName 'DSR_KeyValuePairFile' `
715765
-MockWith { $true } `
@@ -754,7 +804,7 @@ $($script:testAddedName)=$($script:testText)
754804
-ModuleName 'DSR_KeyValuePairFile' `
755805
-Verifiable
756806

757-
Mock `
807+
Mock `
758808
-CommandName Test-Path `
759809
-ModuleName 'DSR_KeyValuePairFile' `
760810
-MockWith { $true } `
@@ -798,7 +848,7 @@ $($script:testAddedName)=$($script:testText)
798848
-ModuleName 'DSR_KeyValuePairFile' `
799849
-Verifiable
800850

801-
Mock `
851+
Mock `
802852
-CommandName Test-Path `
803853
-ModuleName 'DSR_KeyValuePairFile' `
804854
-MockWith { $true } `
@@ -843,7 +893,7 @@ $($script:testAddedName)=$($script:testText)
843893
-ModuleName 'DSR_KeyValuePairFile' `
844894
-Verifiable
845895

846-
Mock `
896+
Mock `
847897
-CommandName Test-Path `
848898
-ModuleName 'DSR_KeyValuePairFile' `
849899
-MockWith { $true } `
@@ -879,6 +929,40 @@ $($script:testAddedName)=$($script:testText)
879929
-Exactly 1
880930
}
881931
}
932+
933+
Context 'File exists and does not contain matching key but it should' {
934+
# verifiable (should be called) mocks
935+
Mock `
936+
-CommandName Assert-ParametersValid `
937+
-ModuleName 'DSR_KeyValuePairFile' `
938+
-Verifiable
939+
940+
Mock `
941+
-CommandName Test-Path `
942+
-ModuleName 'DSR_KeyValuePairFile' `
943+
-MockWith { $false } `
944+
-Verifiable
945+
946+
It 'Should not throw an exception' {
947+
{ $script:result = Test-TargetResource `
948+
-Path $script:testTextFile `
949+
-Name $script:testName.ToUpper() `
950+
-Ensure 'Present' `
951+
-Text $script:testText `
952+
-Verbose
953+
} | Should -Not -Throw
954+
}
955+
956+
It 'Should return false' {
957+
$script:result | Should -Be $false
958+
}
959+
960+
It 'Should call the expected mocks' {
961+
Assert-VerifiableMock
962+
Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1
963+
}
964+
}
965+
882966
}
883967
#endregion
884968

0 commit comments

Comments
 (0)