我有一个返回配置文件内容的函数:
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)
}
}
现在我在Pester中进行了测试,只是检查功能是否实际返回了一些内容:
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"
}
}
}
}
现在,无论我输出配置设置,我都无法通过Pester测试,即使该功能正常运行也很难。
[ - ]应返回值18ms
预期:价值不空 Get-ConfigSettings -path。\ settings-test.ps1 -Rollback&#34; 1&#34; |不应该是BeNullOrEmpty
我在这里遗漏了什么吗?如何正确处理功能输出?
答案 0 :(得分:2)
Get-Help Context
在Context范围的末尾删除了在Context中定义的任何Mocks,
由于It "should fail safely on read Error"
(1)和It "should return a value"
(2)属于同一个Context
块,(1)中定义的Mock Get-Content {throw}
仍然在(2)中有效,所以Get-VA.Settings
不会调用Get-Content
cmdlet,而是调用mock。