Pester可以模拟异常吗?

10

我正在编写一些Pester测试用例,并查看CodeCoverage结果。在大多数包含try/catch的测试用例集中,我们会发现catch部分的代码覆盖率为0%。以下是一个示例:

function Test-Is64Bit()
{

    $Result = $false

    try
    {
        if ((Get-WmiObject -Class "Win32_OperatingSystem").OSArchitecture -eq '64-bit')
        {
            $Result = $true
        }
    }
    catch
    {
        Write-Error $_.Exception | Format-List -Force
    }

    return $Result
}

很容易嘲笑Get-WmiObject返回值来测试$true条件。

我已经尝试了许多想法来模拟从Get-WmiObject引发异常,但在每种情况下,异常都会传递到控制台而不是被Pester捕获并通过测试。以下是我最好的解决方案,但仍然不起作用。

Context "Unit tests for Get-WmiObject exception" {
    # mock the Get-WmiObject cmdlet for unit testing
    Mock Get-WmiObject {
        Throw
    } -ParameterFilter { $Class -And $Class -eq 'Win32_OperatingSystem' }

    It "Function test passes" {
        { Test-Is64Bit } | Should Be $false
        Assert-MockCalled Get-WmiObject -Scope It -Exactly -Times 1
    }
}

这个测试的结果为:

     Context Unit tests for Get-WmiObject error
      [-] Function test passes 138ms
        Expected: {False}
        But was:  { Test-Is64Bit }
        at line: 62 in .\Tests\Test-Is64Bit.Tests.ps1
        62:                             { Test-Is64Bit } | Should Be $false
Tests completed in 590ms
Tests Passed: 4 Failed: 1 Skipped: 0 Pending: 0 Inconclusive: 0

Code coverage report:
Covered 80.00 % of 10 analyzed Commands in 1 File.
Missed commands:

File             Function     Line Command
----             --------     ---- -------
Test-Is64Bit.ps1 Test-Is64Bit   38 Write-Error $_.Exception
Test-Is64Bit.ps1 Test-Is64Bit   38 Format-List -Force

有没有方法可以模拟 Get-WmiObject 抛出异常,以便我们可以让 Pester 进入 catch,并最终实现100%的代码覆盖率?

我目前的测试代码正在寻找异常。

Context "Unit tests for Get-WmiObject exception" {
    # mock the Get-WmiObject cmdlet for unit testing
    Mock Get-WmiObject {
        Throw 'Some Error'
    } -ParameterFilter { $Class -And $Class -eq 'Win32_OperatingSystem' }

    It 'Get-WmiObject should throw' {
        { Get-WmiObject -Class 'Win32_OperatingSystem' } | Should Throw 'Some Error'
    }

    It "Throws exception" {
        { Test-Is64Bit } | Should Throw 'Some Error'
        Assert-MockCalled Get-WmiObject -Scope It -Exactly -Times 1
    }
}

上述代码的结果如下:

     Context Unit tests for Get-WmiObject exception
       [+] Get-WmiObject should throw 89ms
 Test-Is64Bit : System.Management.Automation.RuntimeException: Some Error At
 C:\ONESolutionFinance\Main\ReleaseManagement\PowerShell-Module\SPSNoDeps\Tests\Test-Is64Bit.Tests.ps1:66
 char:7
 +                 { Test-Is64Bit } | Should Throw 'Some Error'
 +                   ~~~~~~~~~~~~
     + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
     + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-Is64Bit

       [-] Throws exception 51ms
         Expected: the expression to throw an exception with message {Some Error}, an exception was not raised, message was {}
             from line:2 char:5
             +                 Throw 'Some Error'
             +                 ~~~~~~~~~~~~~~~~~~
         at line: 66 in C:\ONESolutionFinance\Main\ReleaseManagement\PowerShell-Module\SPSNoDeps\Tests\Test-Is64Bit.Tests.ps1
         66:                             { Test-Is64Bit } | Should Throw 'Some Error'

测试$false的结果如下:

    Context Unit tests for Get-WmiObject exception
      [+] Get-WmiObject should throw 162ms
Test-Is64Bit : System.Management.Automation.RuntimeException: Some Error
At C:\ONESolutionFinance\Main\ReleaseManagement\PowerShell-Module\SPSNoDeps\Tests\Test-Is64Bit.Tests.ps1:66 char:5
+                 Test-Is64Bit | Should Be $false
+                 ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-Is64Bit

      [+] Throws exception 92ms
2个回答

11

可以的!这里有一个例子:

Describe 'Mock an exception' {
    Mock Get-WMIObject { Throw 'some error' }

    It 'Get-WMIObject should throw' {
        {Get-WMIObject -class someclass} | Should Throw 'some error'
    }
}

我认为你的代码无法工作,原因是:

{ Test-Is64Bit } | Should Be $false

应该是:

 { Test-Is64Bit } | Should Throw

在使用Should Throw时,您需要在Should之前使用一个脚本块{ }


你的函数中是否在get-wmiobject命令中使用了“-erroraction stop”?如果没有,可能你的catch语句永远不会被调用。 - Mark Wragg
你是否仍在测试$false结果,而不是抛出异常? - Mark Wragg
或者,如果您的try catch意味着它不会抛出异常,而是返回false,则需要将测试设置为:Test-Is64Bit | Should Be $false - Mark Wragg
我更新了原始帖子,包括实际的 Pester 测试代码和运行结果。 - Dave Wiard
尝试将$Class -And $Class -eq 'Win32_OperatingSystem'更改为$Class -eq 'Win32_OperatingSystem'。前者似乎是不必要的,可能会阻止Mock被调用。我假设Assert-MockCalled也显示了这一点?它在你上面的输出中没有出现。 - Mark Wragg
显示剩余2条评论

0
function Test-LocalFile
{
    param (
        [string]
        $filepath
    )
    try {
        $FileInfo = get-item -Path $filepath -ErrorAction SilentlyContinue
        if ($FileInfo.getType().Name -eq "FileInfo") {
            return $true
        }
    } catch {

         Write-Error -Message "$($_.Exception.Message)"
         Throw
    }
}

这是处理上述定义的函数异常的完整解决方案

Describe "Unit testing for exception handling" {
    Context "unit test showing exception"{

    #arrange
    Mock Write-Error{}
    Mock get-item  {
           Throw 'some error'
    }

    #act
    Write-Error 'some error'
    #assert
    It "test-Local FileThrows exception" {
             {Test-LocalFile -filepath test.txt}  | Should Throw 'some error'
            Assert-MockCalled get-item -Scope It -Exactly -Times 1
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接