Assert-MockCalled

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 } }

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 that the Mock is called that number of times only:

Assert-MockCalled Get-CimInstance -Times 1 -Exactly 
..................Content has been hidden....................

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