-1

構成ファイルの内容を返す関数があります。

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 "1" | BeNullOrEmpty であってはなりません

ここで何か不足していますか?では、関数の出力を適切に処理するにはどうすればよいですか?

4

1 に答える 1

2
Get-Help Context

Context 内で定義されたすべてのモックは、Context スコープの最後で削除されます。

It "should fail safely on read Error"(1) と(2) は同じブロックにIt "should return a value"属しているため、(1) で定義されたものは (2) でも有効であるため、コマンドレットを呼び出すのではなく、代わりにモックを呼び出します。ContextMock Get-Content {throw}Get-VA.SettingsGet-Content

于 2016-01-15T11:44:53.320 に答える