Pester模拟使用-ParameterFilter时未调用Get-Date

问题描述:

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

dummy.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\3.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输出是:因为你使用$f代表-format参数发生

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(和你用你的功能是什么),但似乎对于模拟工作,你需要使用完整参数名称:

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

返回:

Describing dummy 
[+] does something useful 31ms 
+0

这似乎正是问题所在 - 谢谢! – Zoodor