Assert-MockCalled is passing when parameters don't match

65 views
Skip to first unread message

Mark Taylor

unread,
Jun 5, 2017, 6:35:38 AM6/5/17
to Pester
I have the following test:

dummy.Tests.ps1:

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

Describe "dummy" {
    It "does something useful" {
        Mock Set-Content

        dummy

        Assert-MockCalled Set-Content -Times 1 -Exactly -ParameterFilter {
            $Path -eq "C:\foo.txt"
            $Value -eq "baz"
        }
    }
}


dummy.ps1:

function dummy {
    Set-Content -Path "C:\foo.txt" -Value "bar"
}


The test passes, even though the $Value parameters don't match. If I change the $Path that is passed in so that it doesn't match, then it fails as expected.

Any suggestions on what I am doing wrong?

$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


Running on Windows 10.

Chris Dent

unread,
Jun 5, 2017, 6:43:30 AM6/5/17
to Mark Taylor, Pester
Your parameter filter condition should evaluate to true or false. Your usage allows it to generate $true and $false (it evaluates to true) when the filter script block is executed.

Without looking at the source for pester I imagine that returning both true and false will evaluate as true as a single result is true. For the same reason that this does:

{ $true, $false }.Invoke() -eq $true

Change your parameter filter to include a logic operator and the test will work.

        Assert-MockCalled Set-Content -Times 1 -Exactly -ParameterFilter { 
            $Path -eq "C:\foo.txt" -and
            $Value -eq "baz"
        }

--
You received this message because you are subscribed to the Google Groups "Pester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Taylor

unread,
Jun 5, 2017, 6:50:06 AM6/5/17
to Pester, zoo...@gmail.com
Ah that's great - thanks Chris!

I obviously misunderstood how the parameter filter worked.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages