February 2019
Intermediate to advanced
626 pages
15h 51m
English
Pester tracks calls made to mocked commands. The number of times a Mock has been called by a command can be tested by using the Assert-MockCalled command. The following function makes a single call to Get-CimInstance:
function Get-OperatingSystemName {
(Get-CimInstance Win32_OperatingSystem).Caption
}
If a Mock of Get-CimInstance is created, the number of times that the command is called can be tested. In this example, the test asserts that Get-CimInstance is called at least once:
Describe Get-OperatingSystemName { BeforeAll {
Mock Get-CimInstance {
[PSCustomObject]@{
Caption = 'OSName'
}
} } It 'Gets the name of the operating system' {
Get-OperatingSystemName | Should -Be 'OSName' Assert-MockCalled Get-CimInstance } } ...Read now
Unlock full access