【问题标题】:Pester fails test even when exception is caught即使捕获到异常,Pester 也无法通过测试
【发布时间】:2018-05-17 15:47:54
【问题描述】:

我有一个专门处理异常并忽略它的实用程序函数,但是当使用 Pester 测试它时,测试失败,显示已经捕获和处理的异常。我是否遗漏了什么,或者这是 Pester 中的错误?

这段代码重现了这个问题:

function Test-FSPath {

    [cmdletbinding()]
    param([string]$FileSystemPath)

    if([string]::IsNullOrWhiteSpace($FileSystemPath)) { return $false }

    $result = $false
    try {  
        if(Test-Path $FileSystemPath) {
            Write-Debug "Verifying that $FileSystemPath is a file system path"
            $item = Get-Item $FileSystemPath -ErrorAction Ignore
            $result = ($item -ne $null) -and $($item.PSProvider.Name -eq 'FileSystem')        
        }
    } catch {
        # Path pattern that Test-Path / Get-Item can't handle 
        Write-Debug "Ignoring exception $($_.Exception.Message)" 
    }

    return ($result -or ([System.IO.Directory]::Exists($FileSystemPath)) -or ([System.IO.File]::Exists($FileSystemPath)))
}

Describe 'Test' {
    Context Test-FSPath { 
        It 'returns true for a path not supported by PowerShell Test-Path' {
            $absPath = "$env:TEMP\temp-file[weird-chars.txt"
            [System.IO.File]::WriteAllText($absPath, 'Hello world')
            $result = Test-FSPath $absPath -Debug
            $result | Should -Be $true 
            Write-Host "`$result = $result"
            Remove-Item $absPath
        } 
    }
}

预期结果:测试通过

实际结果:测试失败:

[-] returns true for a path not supported by PowerShell Test-Path 2.62s
  WildcardPatternException: The specified wildcard character pattern is not valid: temp-file[weird-chars.txt
  ParameterBindingException: Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: temp-file[weird-chars.txt

【问题讨论】:

  • Test-Path 不应该抛出任何错误,除非你告诉它。您不会抛出任何被捕获的异常,并且您会特别忽略可以执行的操作。在Test-Path上添加-ErrorAction Stop
  • 如果您更改文件名(即,删除“[”)完美!
  • @VictorSilva 这错过了测试的重点。方括号是有效的 Windows 文件路径,Test-Path 无法处理。 Test-FSPath 函数旨在解决此问题

标签: powershell pester


【解决方案1】:

您看到的异常不是来自您的函数,而是来自您对Remove-Item 的使用,它在尝试删除错误路径(也不存在)时抛出错误。您应该删除它,因为您永远不会期望该项目会被创建。

或者,或者(如 cmets 中所述)使用 TestDrive: 然后您无需担心清理(似乎要支持的路径需要使用 $Testdrive)。

    It 'returns true for a path not supported by PowerShell Test-Path' {
        $absPath = "$env:TEMP\temp-file[weird-chars.txt"
        [System.IO.File]::WriteAllText($absPath, 'Hello world')
        $result = Test-FSPath $absPath
        $result | Should -Be $true 
    } 

顺便说一句,我通常倾向于在It 之外做执行类型的东西,然后只测试里面的结果。当我开始为您的代码执行此操作时,它向我显示测试正在通过,因为错误随后转移到发生在 Context 块中。这就是我的意思(这个例子也通过$testdrive变量使用TestDrive:):

Describe 'Test' {
    Context Test-FSPath { 
        $absPath = "$testdrive\temp-file[weird-chars.txt"
        [System.IO.File]::WriteAllText($absPath, 'Hello world')
        $result = Test-FSPath $absPath

        It 'returns true for a path not supported by PowerShell Test-Path' {
            $result | Should -Be $true 
        } 
    }
}

【讨论】:

    猜你喜欢
    • 2022-06-16
    • 1970-01-01
    • 2023-03-13
    • 2017-12-21
    • 2022-11-30
    • 1970-01-01
    • 2013-08-12
    • 2019-08-22
    • 2020-05-13
    相关资源
    最近更新 更多