3

我们正在尝试检查 CmdLetStart-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 已经被调用了一次。所以我假设过滤器中一定有语法错误。

4

1 回答 1

4

问题已解决:

Assert-MockCalled Start-ScheduledTask -Scope it -Times 1 -ParameterFilter {
    ($InputObject.TaskName -eq 'My task')
}
于 2018-02-15T10:09:28.707 回答