【问题标题】:How to properly Pester test Import-Clixml如何正确纠缠测试 Import-Clixml
【发布时间】:2018-05-19 08:54:36
【问题描述】:

所以首先我需要声明我对 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 块的几种变体:

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 应该通过测试,但是当它们不匹配时,它应该失败。

【问题讨论】:

    标签: powershell pester


    【解决方案1】:

    我认为与其使用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 函数的 Mocks,将配置的写入/读取重定向到 TestDrive:\ 这是 Pester 提供并在之后自动清理的特殊临时磁盘区域。

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

    【讨论】:

    • 我不确定我是否理解您的回答,但我确实意识到我最初的问题并没有对实际情况提供见解。我选择 beforeall / afterall 的原因是因为我保存和抓取配置的函数写入预定义路径 ($env:appdata\modulename\modulenameconfig.xml)。我也可以模拟这种情况,因为您的示例假定要输入路径作为所述函数的参数?最重要的是,我想在使用我的函数获取配置后检查存储在 xml 文件中的实际值。这也可能吗?
    • 是的,通过使用 Mock,您可以有效地用不同的代码替换函数。在上面的示例中,我将 Get- 和 Set-Config 函数读取和写入的位置更改为 TestDrive:\ 而不是真实路径。
    • 即使所述函数内部有预定义的路径?你有一些我可以阅读的关于模拟的文档吗?
    • 我接受路径作为参数并不重要,但我会更改我的答案以对其进行硬编码以防万一。
    • 是的,这就是我迄今为止一直在做的事情。总体上可以生成更好的代码。自从我提出这个问题,特别是在 Mocking 上,我学到了很多关于 Pester 的知识。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2021-07-07
    • 2023-01-20
    • 2019-01-03
    相关资源
    最近更新 更多