代码之家  ›  专栏  ›  技术社区  ›  iRon

纠缠5:自动化

  •  0
  • iRon  · 技术社区  · 5 年前

    为了我的爱好项目 ConvertTo-Expression ,我正在重建我的测试(Pester 5)脚本。我想自动化 It (可能还有 Context 部分)因为有大量语法格式需要测试,而且函数实际上是往返的 &([ScriptBlock]::Create("$Expression")) .为了一个 Minimal, Reproducible Example ,我正在使用 ConvertTo-Json 哪趟往返 ConvertTo-Json .
    我这个问题的目标基本上是创建一个 容易的 测试相关功能是否正确往返,例如:

    Test -Compress '{"a":1}'

    我想这样做:

    Function Test ([String]$Json, [Switch]$Compress, [String]$It = $Json) {
        $Context = if ($Compress) { 'Compress' } Else { 'Default' }
        $Object = ConvertFrom-Json $Json
        Context $Context {
    #       BeforeEach {
    #           $Compress = $True
    #           $Json = '{"a":1}'
    #           $Object = ConvertFrom-Json $Json
    #       }
            It $It { ConvertTo-Json -Compress:$Compress -Depth 9 $Object | Should -Be $Json }
        }
    }
    
    Describe 'Syntax check' {
    
        Test -Compress '{"a":1}'
    
    }
    

    但这会导致如下错误:

    Starting discovery in 1 files.
    Discovery finished in 35ms.
    [-] Syntax check.Compress.{"a":1} 15ms (13ms|2ms)
     RuntimeException: The variable '$Compress' cannot be retrieved because it has not been set.
     at <ScriptBlock>, C:\Test.ps1:6
    Tests completed in 140ms
    Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0
    

    启用已注释掉的硬编码 BeforeEach 返回费用 C ted结果:

    Starting discovery in 1 files.
    Discovery finished in 32ms.
    [+] C:\Test.ps1 197ms (80ms|88ms)
    Tests completed in 202ms
    Tests Passed: 1, Failed: 0, Skipped: 0 NotRun: 0
    

    我想把 信息技术 (及 上下文 )在一个函数中,用参数控制它们,这样我就可以做一个简单的测试,比如 测试-压缩“{a”:1}” .不幸的是,我完全迷失在新的Pester发现范围中,开始怀疑这在Pester 5中是否真的可行。

    0 回复  |  直到 5 年前
        1
  •  1
  •   mklement0    5 年前

    我认为问题在于感兴趣的变量只在 嵌套的 脚本块(传递给 It ),而不是外部的(传递给 Context ).

    一个简单的解决方案是调用 .GetNewClosure() 方法,该方法在调用范围的局部变量周围形成一个闭包:

    Function Test ([String]$Json, [Switch]$Compress, [String]$It = $Json) {
        $Context = if ($Compress) { 'Compress' } Else { 'Default' }
        $Object = ConvertFrom-Json $Json
        Context $Context {
            It $It { ConvertTo-Json -Compress:$Compress -Depth 9 $Object | Should -Be $Json }
        }.GetNewClosure() # This captures $Json, $Compress, $It, and $Object
    }
    
    Describe 'Syntax check' {
        Test -Compress '{"a":1}'
    }
    

    请注意 docs 目前的对话非常简洁,但是 GitHub issue #9077 建议返回的脚本块 .GetNewClosure() 在新创建的 动态(内存中)模块 .

        2
  •  0
  •   iRon    5 年前

    我找到了一种使用 -TestCases 参数:

    Function Test ([String]$Json, [Switch]$Compress, [String]$It = $Json) {
        $Context = if ($Compress) { 'Compress' } Else { 'Default' }
        $Object = ConvertFrom-Json $Json
        Context $Context {
            $TestCase = @{ Params = @{ Compress = $Compress; Object = $Object; Json = $Json } }
            It $It -TestCases $TestCase { 
                param($Params)
                ConvertTo-Json -Compress:$Params.Compress -Depth 9 $Params.Object | Should -Be $Params.Json
            }
        }
    }
    
    Describe 'Syntax check' {
    
        Test -Compress '{"a":1}'
    
    }
    

    (但我仍然愿意接受其他建议,尤其是如果我似乎忽略了一些显而易见的事情)