Open
Description
1. General summary of the issue
I couldn't really find an example in the docs so I'll ask my question here.
In the code below it is asserted if the function Get-Process
is called with a specific ParameterFilter
. This works fine but leaves me with the following questions:
- How can we verify if this mock was only called once and not multiple times?
- Would it be possible to name a mock? So we can do something like:
$GetProcessSvcHost = Mock Get-Process -Verifiable -ParameterFilter {
($Name -eq "svchost")
} -MockWith {
[PSCustomObject]@{Name = 'svchost'; Id = 1 }
[PSCustomObject]@{Name = 'svchost'; Id = 2 }
}
. $testScript
Should -InvokeMockName 'GetProcessSvcHost' -Exactly -Times 1
Code example
# Process.ps1
$svcHostProcesses = Get-Process -Name 'svchost'
# Process.Tests.ps1
$testScript = $PSCommandPath.Replace('.Tests.ps1', '.ps1')
Describe 'the script should' {
BeforeAll {
Mock Get-Process
}
It 'get all processes with name svchost' {
Mock Get-Process -Verifiable -ParameterFilter {
($Name -eq "svchost")
} -MockWith {
[PSCustomObject]@{Name = 'svchost'; Id = 1 }
[PSCustomObject]@{Name = 'svchost'; Id = 2 }
}
. $testScript
Should -InvokeVerifiable
$svcHostProcesses.Id | Should -BeExactly @(1, 2)
}
}
2. Describe Your Environment
Pester version : 5.0.4 C:\Program Files\WindowsPowerShell\Modules\Pester\5.0.4\Pester.psd1
PowerShell version : 5.1.14393.3866
OS version : Microsoft Windows NT 10.0.14393.0