From ccd551e7fd36f392e0c062567f8cb67b1c607c2a Mon Sep 17 00:00:00 2001 From: Jakub Jares Date: Thu, 25 Jun 2026 23:27:35 +0200 Subject: [PATCH 1/2] Fix #2672: don't require a verifiable mock that an inner-scope default mock shadows A mock marked -Verifiable in an outer scope that is overridden by a mock for the same command in a narrower scope could never pass Should -InvokeVerifiable, because Get-VerifiableBehaviors still collected the outer behavior and reported it as never invoked even though the inner mock handled every call. Walk the scopes from the test outwards and drop a verifiable behavior when a closer scope has a default mock (no -ParameterFilter) for the same command. A verifiable mock that isn't shadowed is still required. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/functions/Pester.SessionState.Mock.ps1 | 82 +++++++++++++--------- tst/Pester.Mock.RSpec.ts.ps1 | 64 +++++++++++++++++ 2 files changed, 111 insertions(+), 35 deletions(-) diff --git a/src/functions/Pester.SessionState.Mock.ps1 b/src/functions/Pester.SessionState.Mock.ps1 index 142f32d6c..d9499bf9c 100644 --- a/src/functions/Pester.SessionState.Mock.ps1 +++ b/src/functions/Pester.SessionState.Mock.ps1 @@ -366,51 +366,63 @@ function Get-VerifiableBehaviors { $currentTest = Get-CurrentTest $inTest = $null -ne $currentTest - $behaviors = [System.Collections.Generic.List[Object]]@() + # Collect the mock-behavior tables from the closest scope (the test) outwards to the root block, so we can + # walk them in precedence order. A default mock (one without a -ParameterFilter) in a closer scope shadows + # every behavior for the same command in an outer scope, because the closer default catches all calls and + # the outer behaviors can never be invoked. A verifiable behavior that is shadowed like that must not be + # required by Should -InvokeVerifiable, otherwise overriding a verifiable mock in an inner scope always + # fails verification. (#2672) + $behaviorTables = [System.Collections.Generic.List[Object]]@() if ($inTest) { - if ($PesterPreference.Debug.WriteDebugMessages.Value) { - Write-PesterDebugMessage -Scope Mock "We are in a test. Finding all behaviors in this test." - } - $allBehaviors = $currentTest.PluginData.Mock.Behaviors.Values - if ($null -ne $allBehaviors -and $allBehaviors.Count -gt 0) { - # all behaviors for all commands - foreach ($commandBehaviors in $allBehaviors) { - if ($null -ne $commandBehaviors -and $commandBehaviors.Count -gt 0) { - # all behaviors for single command - foreach ($behavior in $commandBehaviors) { - if ($behavior.Verifiable) { - $behaviors.Add($behavior) - } - } - } - } - } + $behaviorTables.Add($currentTest.PluginData.Mock.Behaviors) } $block = Get-CurrentBlock - - # recurse up while ($null -ne $block) { + $behaviorTables.Add($block.PluginData.Mock.Behaviors) + $block = $block.Parent + } - ## action - $allBehaviors = $block.PluginData.Mock.Behaviors.Values - # all behaviors for all commands - if ($null -ne $allBehaviors -or $allBehaviors.Count -ne 0) { - foreach ($commandBehaviors in $allBehaviors) { - if ($null -ne $commandBehaviors -and $commandBehaviors.Count -gt 0) { - # all behaviors for single command - foreach ($behavior in $commandBehaviors) { - if ($behavior.Verifiable) { - $behaviors.Add($behavior) - } - } + $behaviors = [System.Collections.Generic.List[Object]]@() + $shadowedCommands = @{} + + foreach ($behaviorTable in $behaviorTables) { + if ($null -eq $behaviorTable) { + continue + } + + $allBehaviors = $behaviorTable.Values + if ($null -eq $allBehaviors -or $allBehaviors.Count -eq 0) { + continue + } + + # Commands that get a default mock in this scope shadow the same command in outer scopes. Gather them + # and only apply the shadowing after this scope is processed, so behaviors defined together in the same + # scope (e.g. a default and a parameter-filtered mock) don't shadow each other. + $defaultedInThisScope = [System.Collections.Generic.List[string]]@() + + foreach ($commandBehaviors in $allBehaviors) { + if ($null -eq $commandBehaviors -or $commandBehaviors.Count -eq 0) { + continue + } + + $key = "$($commandBehaviors[0].ModuleName)||$($commandBehaviors[0].CommandName)" + $isShadowed = $shadowedCommands.ContainsKey($key) + + foreach ($behavior in $commandBehaviors) { + if ($behavior.Verifiable -and -not $isShadowed) { + $behaviors.Add($behavior) + } + + if ($behavior.IsDefault) { + $defaultedInThisScope.Add($key) } } } - # end action - $block = $block.Parent + foreach ($shadowedKey in $defaultedInThisScope) { + $shadowedCommands[$shadowedKey] = $true + } } - # end $behaviors } diff --git a/tst/Pester.Mock.RSpec.ts.ps1 b/tst/Pester.Mock.RSpec.ts.ps1 index 45f2d75d3..7fe25482f 100644 --- a/tst/Pester.Mock.RSpec.ts.ps1 +++ b/tst/Pester.Mock.RSpec.ts.ps1 @@ -657,6 +657,70 @@ i -PassThru:$PassThru { $t = $r.Containers[0].Blocks[0].Tests[0] $t.Result | Verify-Equal "Passed" } + + t "verifiable mock overridden by a verifiable mock in a narrower scope passes (#2672)" { + $sb = { + Describe 'd' { + BeforeAll { function Get-Data { 'real' } } + Context 'c' { + BeforeAll { Mock Get-Data { 'outer' } -Verifiable } + It 'i' { + Mock Get-Data { 'inner' } -Verifiable + Get-Data | Should -Be 'inner' + Should -InvokeVerifiable + } + } + } + } + + $r = Invoke-Pester -Configuration ([PesterConfiguration]@{ + Run = @{ ScriptBlock = $sb; PassThru = $true } + }) + + $r.Containers[0].Blocks[0].Blocks[0].Tests[0].Result | Verify-Equal "Passed" + } + + t "verifiable mock overridden by a default mock in a narrower scope is not required (#2672)" { + $sb = { + Describe 'd' { + BeforeAll { function Get-Data { 'real' } } + Context 'c' { + BeforeAll { Mock Get-Data { 'outer' } -Verifiable } + It 'i' { + # not verifiable, but a default mock so it shadows the outer verifiable one + Mock Get-Data { 'inner' } + Get-Data | Should -Be 'inner' + Should -InvokeVerifiable + } + } + } + } + + $r = Invoke-Pester -Configuration ([PesterConfiguration]@{ + Run = @{ ScriptBlock = $sb; PassThru = $true } + }) + + $r.Containers[0].Blocks[0].Blocks[0].Tests[0].Result | Verify-Equal "Passed" + } + + t "an uninvoked verifiable mock that is not shadowed still fails InvokeVerifiable" { + $sb = { + Describe 'd' { + BeforeAll { function Get-Data { 'real' } } + It 'i' { + Mock Get-Data { 'mock' } -Verifiable + # never call Get-Data, so the verifiable mock must make InvokeVerifiable fail + Should -InvokeVerifiable + } + } + } + + $r = Invoke-Pester -Configuration ([PesterConfiguration]@{ + Run = @{ ScriptBlock = $sb; PassThru = $true } + }) + + $r.Containers[0].Blocks[0].Tests[0].Result | Verify-Equal "Failed" + } } b "top-level mocks" { From 39036b85acde66c7b288feda88e58f80a3abda51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Wed, 11 Nov 2026 20:20:15 +0100 Subject: [PATCH 2/2] Shadow only a default behavior, a filtered one is still reachable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The shadowing rule dropped every verifiable behavior for a command once a closer scope had a default mock for it, on the grounds that the closer default catches all calls. It does not. FindMatchingBehavior stores the first default it meets and then keeps going, testing every parameter-filtered behavior from every scope, and returns the first one whose filter passes. The default is only the fallback. So an outer verifiable mock with a -ParameterFilter is still reachable from an inner scope that has a default mock, and dropping it made Should -InvokeVerifiable pass while that mock had never been invoked. An assertion passing when it should fail is worse than the bug this fixes, so the rule now only drops a behavior that is itself a default. Two tests: the filtered outer mock is still required when it is not invoked, and it still gets the call when its filter matches. 🤖 --- src/functions/Pester.SessionState.Mock.ps1 | 7 ++- tst/Pester.Mock.RSpec.ts.ps1 | 50 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/functions/Pester.SessionState.Mock.ps1 b/src/functions/Pester.SessionState.Mock.ps1 index cfcf9a9e5..8c39aa52f 100644 --- a/src/functions/Pester.SessionState.Mock.ps1 +++ b/src/functions/Pester.SessionState.Mock.ps1 @@ -1,4 +1,4 @@ -# session state bound functions that act as endpoints, +# session state bound functions that act as endpoints, # so the internal functions can make their session state # consumption explicit and are testable (also prevents scrolling past # the whole documentation :D ) @@ -460,7 +460,10 @@ function Get-VerifiableBehaviors { $isShadowed = $shadowedCommands.ContainsKey($key) foreach ($behavior in $commandBehaviors) { - if ($behavior.Verifiable -and -not $isShadowed) { + # Only a default behavior can be shadowed by a closer default. FindMatchingBehavior tests + # every parameter-filtered behavior from every scope before it falls back to the default it + # stored, so an outer filtered behavior is still reachable and still has to be invoked. + if ($behavior.Verifiable -and -not ($isShadowed -and $behavior.IsDefault)) { $behaviors.Add($behavior) } diff --git a/tst/Pester.Mock.RSpec.ts.ps1 b/tst/Pester.Mock.RSpec.ts.ps1 index 5471ba3d7..d728f75f1 100644 --- a/tst/Pester.Mock.RSpec.ts.ps1 +++ b/tst/Pester.Mock.RSpec.ts.ps1 @@ -721,6 +721,56 @@ i -PassThru:$PassThru { $r.Containers[0].Blocks[0].Tests[0].Result | Verify-Equal "Failed" } + + t "an outer verifiable mock with a -ParameterFilter is still required past an inner default (#2672)" { + # A closer default only shadows an outer *default*. FindMatchingBehavior tests every + # parameter-filtered behavior from every scope before it falls back to the default it + # stored, so this outer filtered mock is still reachable from the test, and never + # calling it has to fail verification. + $sb = { + Describe 'd' { + BeforeAll { function Get-Data { param($Name) 'real' } } + Context 'c' { + BeforeAll { Mock Get-Data { 'outer-filtered' } -ParameterFilter { $Name -eq 'x' } -Verifiable } + It 'i' { + Mock Get-Data { 'inner-default' } + Get-Data -Name 'y' | Should -Be 'inner-default' + Should -InvokeVerifiable + } + } + } + } + + $r = Invoke-Pester -Configuration ([PesterConfiguration]@{ + Run = @{ ScriptBlock = $sb; PassThru = $true } + }) + + $r.Containers[0].Blocks[0].Blocks[0].Tests[0].Result | Verify-Equal "Failed" + } + + t "an outer filtered verifiable mock invoked past an inner default passes (#2672)" { + # The counterpart, and the reason the test above expects a failure: the outer filtered + # behavior really does get the call when its filter matches, inner default or not. + $sb = { + Describe 'd' { + BeforeAll { function Get-Data { param($Name) 'real' } } + Context 'c' { + BeforeAll { Mock Get-Data { 'outer-filtered' } -ParameterFilter { $Name -eq 'x' } -Verifiable } + It 'i' { + Mock Get-Data { 'inner-default' } + Get-Data -Name 'x' | Should -Be 'outer-filtered' + Should -InvokeVerifiable + } + } + } + } + + $r = Invoke-Pester -Configuration ([PesterConfiguration]@{ + Run = @{ ScriptBlock = $sb; PassThru = $true } + }) + + $r.Containers[0].Blocks[0].Blocks[0].Tests[0].Result | Verify-Equal "Passed" + } } b "top-level mocks" {