Reputation: 14735
We're trying to check if the CmdLet Start-ScheduledTask
has been called exactly one time for a specific scheduled task. But for one reason or another the ParameterFilter
is not matched.
Code
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
Function New-TaskObjectsHC {
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[HashTable[]]$Hash
)
Process {
foreach ($H in $Hash) {
$Obj = New-Object -TypeName 'Microsoft.Management.Infrastructure.CimInstance' -ArgumentList @('MSFT_ScheduledTask')
$H.GetEnumerator() | ForEach-Object {
$Obj.CimInstanceProperties.Add([Microsoft.Management.Infrastructure.CimProperty]::Create($_.Key,$_.Value, [Microsoft.Management.Infrastructure.CimFlags]::None))
}
$Obj
}
}
}
Describe 'Monitor sheduled tasks' {
it 'test' {
Mock Get-ScheduledTask {
@(
@{
TaskName = 'My task'
State = 'Running'
}
) | New-TaskObjectsHC
}
Mock Start-ScheduledTask
Start-ScheduledTask -InputObject (Get-ScheduledTask)
Assert-MockCalled Start-ScheduledTask -Scope it -Times 1 -ParameterFilter {
($TaskName -eq 'My task')
}
}
}
When we drop the ParameterFilter
we can clearly see that the CmdLet has been called one time. So I assume there must be a syntax error in the filter.
Upvotes: 3
Views: 690
Reputation: 14735
Problem fixed:
Assert-MockCalled Start-ScheduledTask -Scope it -Times 1 -ParameterFilter {
($InputObject.TaskName -eq 'My task')
}
Upvotes: 4