We're trying to assess if Invoke-Command
has been called exactly one time.
Script.ps1
$job = Invoke-Command -ScriptBlock {'test'} -ComputerName localhost -AsJob
$job | Wait-Job
Script.Tests.ps1
BeforeAll {
$testScript = $PSCommandPath.Replace('.Tests.ps1', '.ps1')
Mock Invoke-Command
}
Describe 'Test' {
It 'should be green' {
. $testScript
Should -Invoke Invoke-Command -Times 1 -Exactly -Scope It
}
}
The problem is mocking the job object in Pester. When the job is not mocked, Wait-Job
will throw an error that it didn't receive a job object.
How is it possible to mock a PowerShell job object in Pester
?