Pester tracks calls made to mocked commands. The number of times a Mock has been called by a command can be tested 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 the command is called can be tested. In this example, the test asserts that Get-CimInstance is called at least once.
Describe Get-OperatingSystemName { Mock Get-CimInstance { [PSCustomObject]@{ Caption = 'OSName' } } It 'Gets the name of the operating system' { Get-OperatingSystemName | Should -Be 'OSName' Assert-MockCalled Get-CimInstance } }
If a test is to verify that a mocked command is never called, the Times parameter of Assert-MockCalled can be set to 0:
Assert-MockCalled Get-CimInstance -Times 0
If a command is used in several different ways, it might be important to ensure that the command is called a specific number of times. In this instance, the Exactly parameter can be added to ensure the Mock is called that number of times only:
Assert-MockCalled Get-CimInstance -Times 1 -Exactly