使用 -ParameterFilter 时未调用 Get-Date 的纠缠模拟

Pester Mock of Get-Date not called when using -ParameterFilter

我创建了一个新的 Pester fixture 并试图模拟对 Get-Date CmdLet 的调用,但它不起作用。如果我不使用 -ParameterFilter.

它确实有效

虚拟。ps1

function dummy {
    return Get-Date -f "dd"
}

dummy.Tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here$sut"

Describe "dummy" {
    Mock Get-Date { return "01" } -Verifiable -ParameterFilter {$f -match "dd"}

    It "does something useful" {
        dummy

        Assert-VerifiableMocks 
    }
}

输出

Describing dummy
 [-] does something useful 99ms
   RuntimeException:  Expected Get-Date to be called with $f -match "dd"
   at Assert-VerifiableMocks, C:\Program Files\WindowsPowerShell\Modules\Pester.4.0\Functions\Mock.ps1: line 434
   at <ScriptBlock>, E:\…\dummy.Tests.ps1: line 11
Tests completed in 99ms
Passed: 0 Failed: 1 Skipped: 0 Pending: 0 Inconclusive: 0

我试过使用 -eq 而不是 -match 作为 -ParameterFilter 没有区别。

我觉得我一定在最基本的层面上做错了什么,但看不到它 - 谁能帮助我?

如果有任何不同,这是在 Windows 10 虚拟机上; $PSVersionTable 的输出是:

Name                           Value                                                                                        
----                           -----                                                                                        
PSVersion                      5.1.14393.1198                                                                               
PSEdition                      Desktop                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                      
BuildVersion                   10.0.14393.1198                                                                              
CLRVersion                     4.0.30319.42000                                                                              
WSManStackVersion              3.0                                                                                          
PSRemotingProtocolVersion      2.3                                                                                          
SerializationVersion           1.1.0.1

出现此问题是因为您使用 $f 表示 -format 参数。 -f-format 的常用缩写(以及您在函数中使用的内容),但似乎要使 Mock 正常工作,您需要使用完整的参数名称:

Mock Get-Date { return "01" } -Verifiable -ParameterFilter {$format -match "dd"}

Returns:

Describing dummy
 [+] does something useful 31ms