Mock .NET calls

The main issue you will encounter while mocking functionality is that Pester cannot mock calls to .NET methods and types. This will add to your code, as you will have to generate wrapper functions around any .NET calls, which you can then mock in your tests:

# This cannot be tested properly
[System.IO.DriveInfo]::new('N')

# This cannot be tested internally as well, but can be mocked
function Get-DriveInfo
{
param
(
$DriveLetter
)

[System.IO.DriveInfo]::new($DriveLetter)
}

Describe SomeTest {
It 'returns $false for IsReady when the drive does not exist' {
Mock Get-DriveInfo {[psobject]@{IsReady = $false}}

(Get-DriveInfo -DriveLetter 'N').IsReady | Should -Be $false
}
}

If your code relies on certain .NET types to be present but you cannot guarantee this, you can add your own types with Add-Type and add the necessary coding. This is usually the case when your module relies on third-party modules or libraries that you cannot assume to be present in your test environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.196.184