Pester可以嘲笑一个例外吗?

Dav*_*ard 8 powershell unit-testing mocking try-catch pester

我正在研究一些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
}
Run Code Online (Sandbox Code Playgroud)

模拟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
    }
}
Run Code Online (Sandbox Code Playgroud)

此测试结果如下:

     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
Run Code Online (Sandbox Code Playgroud)

是否有任何方法可以模拟Get-WmiObject抛出的异常,这样我们就可以让Pester陷入捕获并最终实现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
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码导致了这个:

     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'
Run Code Online (Sandbox Code Playgroud)

测试$ 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
Run Code Online (Sandbox Code Playgroud)

Mar*_*agg 6

是的,它可以!这是一个例子:

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

    It 'Get-WMIObject should throw' {
        {Get-WMIObject -class someclass} | Should Throw 'some error'
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为你的代码不起作用,因为:

{ Test-Is64Bit } | Should Be $false
Run Code Online (Sandbox Code Playgroud)

应该:

 { Test-Is64Bit } | Should Throw
Run Code Online (Sandbox Code Playgroud)

您可以使用一个脚本块{ }之前,Should当你使用Should Throw.