2017-08-08 141 views
7

我工作的一些糾纏測試用例,我期待在代碼覆蓋率結果。在大多數我們的代碼包含try/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可以,但在例外是通過向控制檯,而不是糾纏被抓並通過測試通過每一種情況下例外。下面是我想到的最好的,但它仍然不起作用。

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可以被拋出,所以我們可以有糾纏落入漁獲物和最終實現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' 

測試爲$假返回此:

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 

回答

5

是的,可以!這裏有一個例子:

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之前,當你使用一個腳本塊{ }使用Should Throw

+0

我已經更新了我原來的職位,使問題更加清晰一點。嘲笑一個Get-WmiObject異常並測試Pester中的異常工作得很好。但是,有沒有方法來測試試驗Is64Bit功能,並具有GET-WmiObject可以拋出一個異常,使得糾纏試驗分爲試驗Is64Bit裏面的漁獲物。我仍然得到失敗的測試並查看拋出控制檯的異常。 –

+0

你在函數中使用-erroraction stop get-wmiobject嗎?如果不是,也許你的catch永遠不會被調用? –

+0

我已經在Get-WmiObject上試過並且沒有-ErrorAction停止。控制檯中顯示相同的例外情況。 –

相關問題