2017-06-02 96 views
3

我已經創建了一個新的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 

回答

3

此問題。 -f是一種常用的短手-format(和你用你的功能是什麼),但似乎對於模擬工作,你需要使用完整參數名稱:

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

返回:

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

這似乎正是問題所在 - 謝謝! – Zoodor

相關問題