@@ -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