powershell - 如何正确 Pester 测试 Import-Clixml

标签 powershell pester

因此,首先我需要声明,我对 Pester 还很陌生,可能没有正确编写我的测试,或者没有正确理解其所有功能。

背景是我想使用 Pester 自动化我的 PowerShell 模块,并且到目前为止已经编写了一些测试。

我的模块的一部分是将配置内容保存在 clixml 文件中。我想编写一组测试来确保保存和获取配置按预期工作。

基本上,我有一个保存配置文件的函数和一个检索它的函数。我的 Pester 测试如下所示:

BeforeAll{
        if(Test-path existingconfigfile.xml){

            Rename-Item -Path "existingconfigfile" -NewName "backup.xml"
        }

        Save-configfunction -param1 'Value1' -param2 'Value2'
        #saves as test.xml
    }

    Afterall{
        if(Test-path backup.xml){

            # Remove mocked test file
            Remove-Item -Path "test.xml" -Force

            # Place original back
            Rename-Item -Path "backup.xml" -NewName "existingconfigfile.xml"
        }
    }

    it "importconfig should return expected values for mocked object" {

        {
            $result = Get-config
            $result
            $result.Containsvalue('Value1') | Should be $true

        }
    }

现在我尝试了 it block 的几种变体:

it "importconfig should return expected values for mocked object" {

        {
            $result = Get-config
            $result.param1 | Should be "Value1"
        }
    }

    it "importconfig should return expected values for mocked object" {
        $result = Get-Config

        $result | Should match 'Value1'
        $result | Should match 'Value2'
    }

    it "importconfig should return expected values for mocked object" {

        $result = Get-Config

        $result.Param1 | Should match 'Value1'
        $result.Param2 | Should match 'Value2'
    }

即使我将匹配值更改为不正确的值,Pester 也始终返回通过的测试。 Pester 在所有情况下都会这样做。因此,由于某种原因,Pester 无法正确限定值,并且始终返回正结果。

所以我想知道我做错了什么。显然,如果值确实匹配,Pester 应该通过测试,但如果它们不匹配,则应该失败。

最佳答案

我认为,我不会使用 BeforeAllAfterAll 来创建 Mock 类型行为来修改配置,而是使用实际的 Mock 语句。这就是我的意思(我创建了简单的表示,表示我假设您的函数会执行的操作,因为您尚未共享它们):

function Set-Config {
    Param(
        $Config
    )
    $Config | Export-Clixml C:\Temp\production_config.xml
}

function Get-Config {
    Import-Clixml C:\Temp\production_config.xml
}

Describe 'Config function tests' {

    Mock Set-Config {
        $Config | Export-Clixml TestDrive:\test_config.xml
    }

    Mock Get-Config {
        Import-Clixml TestDrive:\test_config.xml
    }

    $Config = @{
        Setting1 = 'Blah'
        Setting2 = 'Hello'
    }

    It 'Sets config successfully' {
        { Set-Config -Config $Config } | Should -Not -Throw
    }

    $RetrievedConfig = Get-Config

    It 'Gets config successfully' {
        $RetrievedConfig.Setting1 | Should -Be 'Blah'
        $RetrievedConfig.Setting2 | Should -Be 'Hello'
    }
}

这将创建 Get-ConfigSet-Config 函数的模拟,将配置的写入/读取重定向到 TestDrive:\这是一个特殊的临时磁盘区域Pester 提供并随后自动清理。

请注意,这仅在测试使用这些函数的父函数时才有意义。如果您正在编写 Get-ConfigSet-Config 函数本身的测试,那么您可能会想要模拟 Export-CliXmlImport-CliXml 命令。

关于powershell - 如何正确 Pester 测试 Import-Clixml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423508/

相关文章:

json - 在另一个Powershell脚本文件中调用Powershell脚本时如何使用嵌套引号

powershell - 使用 pester 测试 powershell DSC 脚本类 - 找不到类型 [ClassName]

powershell - 引发预期错误时,Pester测试失败

powershell - 如何为多个小数点的值范围编写Pester单元测试

arrays - Select-Object -Unique返回String而不是String数组

powershell - 如何在 PowerShell 中实现 '-Help' 开关?

unit-testing - 纠缠和测试枚举

powershell - 使用 -ParameterFilter 时未调用 Get-Date 的纠缠模拟

c# - 从 PowerShell WebJob 读取 Azure WebApp ApplicationSetting

Azure PowerShell : Get-AzVM does not display the output