33InModuleScope ' PSKoans' {
44 Describe " Show-Advice" {
55
6- BeforeAll{
7- Mock Write-ConsoleLine { }
6+ BeforeAll {
7+ Mock Write-ConsoleLine { }
88 # Exporting incorrect advices to the folder
99 $AdviceFolder = $script :ModuleRoot | Join-Path - ChildPath ' Data/Advice'
1010 $AdviceObject = Get-ChildItem - Path $AdviceFolder - Recurse - File - Filter " *.Advice.json"
1111 $RandomAdvicesFilePaths = ($AdviceObject | Get-Random - Count 3 ).FullName
12- $Backup = $RandomAdvicesFilePaths | ForEach-Object {Get-Content $_ | ConvertFrom-Json }
12+ $Backup = $RandomAdvicesFilePaths | ForEach-Object { Get-Content $_ | ConvertFrom-Json }
1313 }
1414
1515 Context " Behaviour of Parameter-less Calls" {
@@ -22,7 +22,7 @@ InModuleScope 'PSKoans' {
2222 It " calls Write-ConsoleLine with only the display string" {
2323 Assert-MockCalled - CommandName Write-ConsoleLine - ParameterFilter { $null -ne $Title } - Times 1
2424 }
25-
25+
2626 It " outputs nothing to the pipeline" {
2727 $result | Should - BeNullOrEmpty
2828 }
@@ -31,51 +31,57 @@ InModuleScope 'PSKoans' {
3131 Context " Behaviour with -Name Parameter" {
3232
3333 It " should call Write-ConsoleLine with normal parameters" {
34- Show-Advice - Name " Profile"
34+ Show-Advice - Name " Profile"
3535 Assert-MockCalled - CommandName Write-ConsoleLine - ParameterFilter { $null -ne $Title }
3636 }
3737
3838 It " should call Write-ConsoleLine without parameters" {
39- Show-Advice - Name " Profile"
39+ Show-Advice - Name " Profile"
4040 Assert-MockCalled - CommandName Write-ConsoleLine - ParameterFilter { $null -eq $Title }
4141 }
4242
4343 It " should throw an error if the requested file cannot be found" {
4444 $message = " Could not find any Advice files matching the specified Name: ThisDoesntExist"
4545 { Show-Advice - Name " ThisDoesntExist" - ErrorAction Stop } | Should - Throw - ExpectedMessage $Message
4646 }
47+ }
4748
48- # Creating incorrect Advice
49- $IncorrectObjectsData = @ (
50- @ {
51- NotTitle = " Fake title"
52- NotContent = @ (1 .. 4 | ForEach-Object {" Fake line $_ " })
53- },
54- @ {
55- Content = @ (1 .. 4 | ForEach-Object {" Fake line $_ " })
56- },
57- @ {
58- Title = " Fake title"
59- }
60- )
61- for ($i = 0 ; $i -lt 3 ; $i ++ ) {
62- $IncorrectObjectsData [$i ] | ConvertTo-Json | Out-File $RandomAdvicesFilePaths [$i ]
63- }
64-
65- It " should throw an error if the requested file's format is not correct" {
66- for ($i = 0 ; $i -lt 3 ; $i ++ ) {
67- $AdviceName = (Get-Item $RandomAdvicesFilePaths [$i ]).BaseName -replace (' ^(.*)(?:.Advice)' , ' $1' )
68- $Message = " Could not find Title and/or Content elements for Advice file: {0}" -f $AdviceName
69- { Show-Advice - Name $AdviceName - ErrorAction Stop } | Should - Throw - ExpectedMessage $Message
70- }
49+ Context ' Behaviour with malformed advice files' {
50+
51+ BeforeAll {
52+ $MalformedAdviceCases = @ (
53+ @ {
54+ Json = @ {
55+ NotTitle = " Fake title"
56+ NotContent = @ (1 .. 4 | ForEach-Object { " Fake line $_ " })
57+ } | ConvertTo-Json
58+ }
59+ @ {
60+ Json = @ {
61+ Content = @ (1 .. 4 | ForEach-Object { " Fake line $_ " })
62+ } | ConvertTo-Json
63+ }
64+ @ {
65+ Json = @ {
66+ Title = " Fake title"
67+ } | ConvertTo-Json
68+ }
69+ )
70+
71+ $GetContentResult = [string ]::Empty
72+
73+ Mock Get-Content - MockWith { $GetContentResult }
74+ Mock Get-ChildItem - MockWith { [PSCustomObject ]@ { PSPath = " DummyPath" } }
7175 }
7276
73- }
77+ It " should throw an error if the requested file's format is not correct" - TestCases $MalformedAdviceCases {
78+ param ($Json )
7479
75- AfterAll{
76- for ($i = 0 ; $i -lt 3 ; $i ++ ) {
77- Set-Content - Path ((Get-Item $RandomAdvicesFilePaths [$i ]).FullName) - Value ($Backup [$i ] | ConvertTo-Json )
80+ $GetContentResult = $Json
81+ $AdviceName = " TestAdvice"
82+ $Message = " Could not find Title and/or Content elements for Advice file: {0}" -f $AdviceName
83+ { Show-Advice - Name $AdviceName - ErrorAction Stop } | Should - Throw - ExpectedMessage $Message
7884 }
7985 }
80- }
86+ }
8187}
0 commit comments