首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确处理Pester测试Import

如何正确处理Pester测试Import
EN

Stack Overflow用户
提问于 2018-05-19 08:54:36
回答 1查看 352关注 0票数 2

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

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

模块的一部分是在clixml文件中保存配置内容。我想编写一组测试,以确保保存和捕获配置按照预期的方式工作。

基本上,我有一个函数来保存配置文件,另一个函数用来检索它。我的Pester测试看起来如下:

代码语言:javascript
复制
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块的几个变体:

代码语言:javascript
复制
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应该通过测试,如果值实际匹配,但当它们不匹配时,它应该失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-19 09:42:54

我认为,与其使用BeforeAllAfterAll来创建用于修改配置的Mock类型行为,不如使用实际的Mock语句。这里是我的意思(我已经创建了假设您的函数所做的事情的简单表示,因为您还没有共享它们):

代码语言:javascript
复制
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命令。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50423508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档