纠缠 ParameterFilter 与 Assert-MockCalled 不匹配

Pester ParameterFilter not matched by Assert-MockCalled

我们正在尝试检查 CmdLet Start-ScheduledTask 是否已为特定计划任务准确调用一次。但是由于某种原因 ParameterFilter 不匹配。

代码

$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')
        }
    }
}

当我们放下 ParameterFilter 时,我们可以清楚地看到 CmdLet 已被调用一次。所以我假设过滤器中一定存在语法错误。

问题已解决:

Assert-MockCalled Start-ScheduledTask -Scope it -Times 1 -ParameterFilter {
    ($InputObject.TaskName -eq 'My task')
}