DEV Community

Discussion on: Unit testing in PowerShell, introduction to Pester

xero399 profile image

First of all, thanks you Olivier for this AWESOME introduction to Pester!

I'm trying to mock a function but isn't working as expected. The mocking is:

Mock Get-PnPList{return @{"Title"="testList"}} -ParameterFilter { $Identity -eq "testList" }
Mock Get-PnPList{return $null } -ParameterFilter { $Identity -ne "testList" }

The script code is:

function checkListExists{
Param([Parameter(Mandatory = $true)][string] $listTitle)
$list = Get-PnPList -Identity $listTitle
if($null -eq $list){
return $false
return $true

And finally I'm calling

checkListExists "testList" | Should -BeTrue

But the test fails: "Expected $true, but got $false."

I'm debugging and the parameter -Identity $listTitle of the Get-PnPList function is receiving the value "testList".

Why the moking is not working as expected?


omiossec profile image
Olivier Miossec Author

I plan to write a post about mocking in Pester

You should try to do somehitng like that

Mock Get-PnPList -MockWith {
"Title" = "testList"
} -ParameterFilter { $Identity -eq "testList" }

xero399 profile image

Hi, thanks for the answer. Tried like you said but doesn't work. Finally I have found that the problem was the -ParameterFilter. Doing $Identity.toString() -eq "testList" it works perfectly. That .toString() makes the difference!