Skip to content

Conversation

nohwnd
Copy link
Member

@nohwnd nohwnd commented Jul 3, 2025

PR Summary

Removes fallback to the mocked command. And replaces it with error message .

import-module .\bin\Pester.psd1 -force

Invoke-Pester -Container (New-PesterContainer -ScriptBlock { 
Describe "a" {
    It "b" {
        function FunctionUnderTestWithoutParams ()  {}
        Mock FunctionUnderTestWithoutParams { return "fake results" } -parameterFilter { $param1 -eq "test" -and $args[0] -eq 'notArg0' }
        { FunctionUnderTestWithoutParams -param1 "test" "arg0" } | Should -Throw "Mock felt though"


    }
    }
}) -Output Diagnostic

 [-] b 3s (3s|3ms)
   Expected an exception with message like 'Mock felt though' to be thrown, but the message was 'The mock for command 'FunctionUnderTestWithoutParams' did not match any filtered behavior, and there was no default behavior.
   Performed ParameterFilter evaluations:
   mock filter: {  $param1 -eq "test" -and $args[0] -eq 'notArg0'  } without any parameters
   '. from S:\p\pester\src\functions\Mock.ps1:863 char:17
       + …             throw "The mock for command '$($Hook.CommandName)' did no …
       +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   at { FunctionUnderTestWithoutParams -param1 "test" "arg0" } | Should -Throw "Mock felt though", untitled:Untitled-1:8

Fix #2178

@nohwnd
Copy link
Member Author

nohwnd commented Jul 3, 2025

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
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
1 participant