# . $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" $originalMachineName = $env:COMPUTERNAME Describe "Get-ConfigSetting" { Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith { return "SUT"} Mock -ModuleName $moduleForMock -CommandName Write-Verbose -MockWith { } Context "No Config Setting present returns `$null" { Mock -ModuleName $moduleForMock -CommandName Get-AppSetting -MockWith { return $null } Mock -ModuleName $moduleForMock -CommandName Get-EnvironmentVariable -MockWith { return $null } $response = Get-ConfigSetting "anything" It "should be `$null" { $response | Should -BeNull } It "tries to get the app setting" { Assert-MockCalled -CommandName Get-AppSetting -ModuleName $moduleForMock -Times 1 -Exactly -Scope Context } It "tries to get the environment variable" { Assert-MockCalled -CommandName Get-EnvironmentVariable -ModuleName $moduleForMock -Times 1 -Exactly -Scope Context } } Context "Environment Variable Setting present" { Mock -ModuleName $moduleForMock -CommandName Get-AppSetting -MockWith { return $null } Mock -ModuleName $moduleForMock -CommandName Get-EnvironmentVariable -MockWith { return "a value" } $response = Get-ConfigSetting "anything" It "should be the target value" { $response | Should -Be "a value" } It "tries to get the app setting" { Assert-MockCalled -CommandName Get-AppSetting -ModuleName $moduleForMock -Times 1 -Exactly -Scope Context } It "tries to get the environment variable" { Assert-MockCalled -CommandName Get-EnvironmentVariable -ModuleName $moduleForMock -Times 1 -Exactly -Scope Context } } Context "Machine Config Setting present" { Mock -ModuleName $moduleForMock -CommandName Get-AppSetting -MockWith { return "a value" } Mock -ModuleName $moduleForMock -CommandName Get-EnvironmentVariable -MockWith { return $null } $response = Get-ConfigSetting "anything" It "should be the target value" { $response | Should -Be "a value" } It "tries to get the app setting" { Assert-MockCalled -CommandName Get-AppSetting -ModuleName $moduleForMock -Times 1 -Exactly -Scope Context } It "does not try to get the environment variable" { Assert-MockCalled -CommandName Get-EnvironmentVariable -ModuleName $moduleForMock -Times 0 -Exactly -Scope Context } } }