Pester ParameterFilter与Assert-MockCalled不匹配

时间:2018-02-15 10:02:49

标签: powershell mocking assert pester

我们正在尝试检查CmdLet Hashtable是否已针对特定计划任务调用了一次。但由于某种原因,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') } } } 时,我们可以清楚地看到CmdLet已被调用一次。所以我假设过滤器中必定存在语法错误。

1 个答案:

答案 0 :(得分:1)

问题已解决:

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