Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,34 @@ Describe "When calling Mock on non-existing function" {
}
}

Describe 'When calling Mock, StrictMode is enabled, and variables are used in the ParameterFilter' {
Set-StrictMode -Version Latest

$result = $null
$testValue = 'test'

try
{
Mock FunctionUnderTest { 'I am the mock' } -ParameterFilter { $param1 -eq $testValue }
}
catch
{
$result = $_
}

It 'Does not throw an error when testing the parameter filter' {
$result | Should Be $null
}

It 'Calls the mock properly' {
FunctionUnderTest $testValue | Should Be 'I am the mock'
}

It 'Properly asserts the mock was called when there is a variable in the parameter filter' {
Assert-MockCalled FunctionUnderTest -Exactly 1 -ParameterFilter { $param1 -eq $testValue }
}
}

Describe "When calling Mock on existing function without matching bound params" {
Mock FunctionUnderTest {return "fake results"} -parameterFilter {$param1 -eq "test"}

Expand Down
7 changes: 4 additions & 3 deletions Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,9 @@ function Test-ParameterFilter
if ($null -eq $ArgumentList) { $ArgumentList = @() }
if ($null -eq $CmdletBinding) { $CmdletBinding = '' }
if ($null -eq $ParamBlock) { $ParamBlock = '' }

$cmd = [scriptblock]::Create("$CmdletBinding `r`n param ( $ParamBlock ) `r`n$ScriptBlock")


$cmd = [scriptblock]::Create("$CmdletBinding param ( $ParamBlock ) Set-StrictMode -Off; $ScriptBlock")
Set-ScriptBlockScope -ScriptBlock $cmd -SessionState $pester.SessionState

& $cmd @BoundParameters @ArgumentList
}