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

I created a new Pester appliance and am trying to mock the Get-Date

CmdLet call , but it doesn't work. It works if I don't use -ParameterFilter

.

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 
    }
}

      

Output

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

      

I tried using -eq

instead -match

for for -ParameterFilter

no difference.

I feel like I must be doing something wrong at a very basic level, but I can't see it - can someone help me?

If it matters, it's on a Windows 10 virtual machine; output from $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

      

+3


source to share


1 answer


This issue occurs because of what you are using $f

to represent the parameter -format

. -f

is a widely used shorthand for -format

(and what you use in your function), but it looks like you need to use fully qualified parameter names for Mock to work:

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

      



Return:

Describing dummy
 [+] does something useful 31ms

      

+4


source







All Articles