Skip to content

Remove Mock fallthrough #2654

Merged
nohwnd merged 2 commits into
mainfrom
remove-mock-fallthrough
Jun 16, 2026
Merged

Remove Mock fallthrough #2654
nohwnd merged 2 commits into
mainfrom
remove-mock-fallthrough

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 3, 2025

Copy link
Copy Markdown
Member

PR Summary

Mock no longer falls through to the original command when no behavior matches. It throws instead. Fix #2178.

Invoke-Pester -Container (New-PesterContainer -ScriptBlock {
    Describe "a" {
        It "b" {
            function FunctionUnderTest ($param1) {}
            Mock FunctionUnderTest { "fake" } -ParameterFilter { $param1 -eq "test" }
            FunctionUnderTest -param1 "other"
        }
    }
})
[-] b
  RuntimeException: No mock for command 'FunctionUnderTest' matched the call: none of
  the parameter filters matched, and there is no default mock to fall back to. Add a
  default mock (e.g. `Mock FunctionUnderTest { ... }`) or adjust an existing -ParameterFilter.

  The following parameter filters were evaluated and did not match:
      mock filter: {  $param1 -eq "test"  } without any parameters

A separate message points at the bootstrap-leak case (outer Mock leaking into a nested
Invoke-Pester whose own mock table is empty) since there are no filters to show in that path.

@nohwnd

nohwnd commented Jul 3, 2025

Copy link
Copy Markdown
Member Author

The error message is pretty terrible. We will see it only when there is no default behavior, so there must be at least 1 paramtrized mock.

I tried to show what we evaluated in order to reach that point where no filter matched. but the wording needs to be better, and well as the formatting of the invoked filters I think.

But then we should be able to re-use similar thing for Should -Invoke to show the history of execution, when failing should Invoke. To show the performed invocations.

@nohwnd nohwnd changed the title Mock fallthrough Remove Mock fallthrough Jul 3, 2025
@nohwnd nohwnd force-pushed the remove-mock-fallthrough branch from 20b9cdd to 29d1756 Compare June 12, 2026 12:38
Comment thread src/functions/Mock.ps1 Fixed
Comment thread src/functions/Mock.ps1 Fixed
@nohwnd nohwnd marked this pull request as ready for review June 12, 2026 12:58
@nohwnd nohwnd force-pushed the remove-mock-fallthrough branch from 29d1756 to 349ed05 Compare June 12, 2026 13:40
@nohwnd

nohwnd commented Jun 12, 2026

Copy link
Copy Markdown
Member Author

Removed the orphan-bootstrap fallthrough entirely at 349ed05Invoke-MockInternal no longer ever invokes the original command. The NoBehaviors branch and its 30+ lines of original-command setup are gone.

What happens now in the leaked-bootstrap case (outer Mock Get-Date {1} + nested Invoke-Pester where the inner run calls Get-Date without its own mock): throws with a clear, actionable message:

No mock for command 'Get-Date' is defined in this scope, but the bootstrap is active (typically a Mock from an outer scope leaked into a nested Invoke-Pester run). Add a Mock for 'Get-Date' in this scope, or restructure the test so the outer Mock does not leak.

The pre-existing nested-Pester test (Describe 'Mocking with nested Pester runs') is updated to assert the throw instead of expecting silent fallthrough. The inner local mock works test still passes.

Also fixed the two PSScriptAnalyzer warnings: ForEach-Object calls in Mock.ps1 now go through & $SafeCommands['ForEach-Object'].

229/229 in Mock.Tests, 2183/0 full suite green on PS 7.6.

@nohwnd nohwnd force-pushed the remove-mock-fallthrough branch from 3ef646b to c5422e1 Compare June 12, 2026 13:50
@nohwnd nohwnd force-pushed the remove-mock-fallthrough branch from c5422e1 to 2b0ddf6 Compare June 12, 2026 14:00
Doc-comments and debug messages in Pester.SessionState.Mock.ps1 still
described the old behavior (calling through to the original command).
Updated to reflect that unmatched calls now throw.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd enabled auto-merge (squash) June 16, 2026 16:13
@nohwnd nohwnd merged commit fadc0f1 into main Jun 16, 2026
13 checks passed
@nohwnd nohwnd deleted the remove-mock-fallthrough branch June 16, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Pester 5] Original function silently executed if parameter validation fails in mock

2 participants