How to mock a job in Pester? How to mock a job in Pester? powershell powershell

How to mock a job in Pester?


One solution might be to have the Mock of Invoke-Command still create a legitimate job, but executing some script/code that you deem safe for the purpose of testing.

To do this, you need to first put the Invoke-Command cmdlet in a variable so that you can use it via that variable (because a Mock can't directly call its own command).

For example:

$InvokeCommand = Get-Command Invoke-CommandMock Invoke-Command {     & $InvokeCommand -ScriptBlock {'some safe alternative code'} -ComputerName localhost -AsJob}