Tôi đang làm việc trên một số trường hợp thử nghiệm Pester và tôi đang xem kết quả CodeCoverage. Trong hầu hết các trường hợp thử nghiệm mà chúng tôi mã chứa thử/bắt, chúng tôi nhận được mức độ phù hợp 0% khi bắt. Dưới đây là ví dụ:Pester có thể giả lập một ngoại lệ?
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
}
Thật dễ dàng để giả lập giá trị trả về Get-WmiObject để kiểm tra điều kiện thực tế $.
Tôi đã thử một số ý tưởng để giả mạo ngoại lệ từ Get-WmiObject nhưng trong mọi trường hợp ngoại lệ được chuyển qua bảng điều khiển thay vì bị Pester bắt và vượt qua bài kiểm tra. Dưới đây là tốt nhất tôi đã đưa ra nhưng nó vẫn không hoạt động.
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
}
}
kiểm tra này kết quả trong:
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
có phải là cách nào để chế giễu một ngoại lệ được ném bởi Get-WmiObject vì vậy chúng tôi có thể có quấy rầy rơi vào đánh bắt và cuối cùng đạt được mã số bảo hiểm 100%?
mã kiểm tra hiện tại của tôi tìm kiếm ngoại lệ
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
}
}
Kết quả mã trên trong việc này:
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'
Testing với $ false trả này:
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
Tôi đã cập nhật bài đăng gốc của mình để làm cho câu hỏi rõ ràng hơn một chút. Mocking một Get-WmiObject ngoại lệ và thử nghiệm cho các ngoại lệ trong Pester hoạt động tốt. Nhưng có một cách để kiểm tra chức năng Test-Is64Bit và có Get-WmiObject ném một ngoại lệ sao cho kiểm tra Pester rơi vào bẫy bên trong Test-Is64Bit. Tôi vẫn nhận được các kiểm tra không thành công và xem các ngoại lệ được ném vào bảng điều khiển. –
Bạn có đang sử dụng tính năng dừng -erroraction trên get-wmiobject trong chức năng của bạn không? Nếu không, bạn không bao giờ bị bắt? –
Tôi đã thử cả hai có và không có sự dừng -ErrorAction trên Get-WmiObject. Ngoại lệ tương tự cũng được hiển thị trong bảng điều khiển. –