. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.json\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Get-Module $sut | Remove-Module -Force $moduleForMock = "Alkami.PowerShell.Configuration" Describe "Set-AppSetting.json" { $configPathUUT = "TestDrive:\appsettings.json" Context "Writes an Error and Returns False if the appSettings Node can't take children elements" { $appSettingsTestContents = @" { "FakeSetting": "FakeValue" } "@ Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} $appSettingsTestContents | Out-File $configPathUUT -Force Set-AppSetting -Path $configPathUUT -Key "FakeSetting:SubSetting" -Value 'test' It "did write an error" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 1 -Scope Context } It "did not write a warning to the console" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 0 -Scope Context } It "did not output useless information" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 0 -Scope Context } } Context "App Setting Key Does Not Exist with -UpdateOnly" { $appSettingsTestContents = @" { } "@ Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} $appSettingsTestContents | Out-File $configPathUUT -Force Set-AppSetting -Path $configPathUUT -Key "MissingSetting" -Value 'test' -UpdateOnly It "does not create a missing AppSetting key with -UpdateOnly" { $value | Should -BeNull } It "did not write an error" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context } It "did write a warning to the console" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 1 -Scope Context } It "did not output useless information" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 0 -Scope Context } } Context "Does a happy path - simple" { $appSettingsTestContents = @" { "FakeSetting": "FakeValue" } "@ Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} $appSettingsTestContents | Out-File $configPathUUT -Force Set-AppSetting -Path $configPathUUT -Key "FakeSetting" -Value 'test' $value = Get-AppSetting -Path $configPathUUT -key "FakeSetting" It "still got the right value" { $value | Should -Be 'test' } It "did not throw an error" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context } It "Wrote no warnings" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 0 -Scope Context } It "Showed simple verification" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 2 -Scope Context } } Context "Does a happy path - complex" { $appSettingsTestContents = @" { "FakeSetting": { "FakeSubchild": "FakeValue" } } "@ Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} $appSettingsTestContents | Out-File $configPathUUT -Force Set-AppSetting -Path $configPathUUT -Key "FakeSetting:FakeSubchild" -Value 'test' $value = Get-AppSetting -Path $configPathUUT -key "FakeSetting:FakeSubchild" It "still got the right value" { $value | Should -Be 'test' } It "did not throw an error" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context } It "Wrote no warnings" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 0 -Scope Context } It "Showed simple verification" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 2 -Scope Context } } Context "Does a happy path - more complex" { $appSettingsTestContents = @" { "FakeSetting": { "FakeMiddle": { "FakeSubchild": "FakeValue" } } } "@ Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} $appSettingsTestContents | Out-File $configPathUUT -Force Set-AppSetting -Path $configPathUUT -Key "FakeSetting:FakeMiddle:FakeSubchild" -Value 'test' $value = Get-AppSetting -Path $configPathUUT -key "FakeSetting:FakeMiddle:FakeSubchild" It "still got the right value" { $value | Should -Be 'test' } It "did not throw an error" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context } It "Wrote no warnings" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 0 -Scope Context } It "Showed simple verification" { Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 2 -Scope Context } } }