I have a function which returns the contents of the a config file:
function Get-VA.Settings {
<#
.SYNOPSIS
Fetches settings from a XML file
.DESCRIPTION
Fetches settings from a XML file and outputs a XML Object
.EXAMPLE
Get-VA.Settings -path <path-to-config-file> -Rollback <path-to-rollback-file>
#>
Param (
[Parameter(Mandatory=$true,Position=0)]
[string]$path,
[Parameter(Mandatory=$true,Position=1)]
[string]$Rollback
)
try {
[xml]$config = Get-Content -Path $path
Write-Output -InputObject $config
} catch {
Write-VA.EventLog -Message ("Could not load Configuration File: `r`n" + $Error) -Id 11 -type Error
Invoke-VA.Rollback -Path $($Rollback)
}
}
Now I have a test in Pester which just check if the funtion actually returns something:
Import-Module ($PSScriptRoot + "\utility.psm1")
Describe "Settings Module" {
InModuleScope utility {
Context "Get-VA.Settings" {
It "should have Help and Examples" {
$helpinfo = Get-Help Get-VA.Settings
$helpinfo.examples | should not BeNullOrEmpty # should have examples
$helpinfo.details | should not BeNullOrEmpty # should have Details
$helpinfo.description | Should not BeNullOrEmpty # Should have a Description for the Function
}
It "should fail safely on read Error" {
Mock Get-Content {throw}
Mock Write-VA.EventLog { }
Mock Invoke-VA.Rollback { }
Get-VA.Settings -path "1" -Rollback "1"
Assert-MockCalled Invoke-VA.Rollback -Times 1
}
It "should return a value" {
Set-Content -Value "<xml><foo>bar</foo></xml>" -Path "settings-test.ps1"
Get-VA.Settings -path .\settings-test.ps1 -Rollback "1" | should not BeNullOrEmpty
Remove-Item "settings-test.ps1"
}
}
}
}
Now whatever I do to output the configuration settings I can't seem to pass the test with Pester even tough the function properly runs.
[-] should return a value 18ms
Expected: value to not be empty
Get-ConfigSettings -path .\settings-test.ps1 -Rollback "1" | should not BeNullOrEmpty
Am I missing something here? How do I handle function output properly then?
Get-Help Context
Any Mocks defined inside a Context are removed at the end of the Context scope,
Since It "should fail safely on read Error"
(1) and It "should return a value"
(2) belong to same Context
block, Mock Get-Content {throw}
defined in (1) still have effect in (2), so that Get-VA.Settings
does not call Get-Content
cmdlet, but call mock instead.