Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should-InvokeInternal is missing the -Because parameter #2121

Closed
jazzdelightsme opened this issue Dec 29, 2021 · 2 comments · Fixed by #2229
Closed

Should-InvokeInternal is missing the -Because parameter #2121

jazzdelightsme opened this issue Dec 29, 2021 · 2 comments · Fixed by #2229

Comments

@jazzdelightsme
Copy link

General summary of the issue

The Should-InvokeInternal worker function is missing the $Because parameter. The Should-Invoke function does have the -Because parameter, and it turns around and passes it to Should-InvokeInternal by splatting in $PSBoundParameters:

    $result = Should-InvokeInternal @PSBoundParameters

But that results in a ParameterBindingException:

Exception             : System.Management.Automation.ParameterBindingException: A parameter cannot be found that matches parameter name 'Because'.
                           at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
                           at Should-Invoke(Closure , FunctionContext )
                           at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
                           at System.Management.Automation.CommandProcessorBase.Complete()
TargetObject          :
CategoryInfo          : InvalidArgument: (:) [Should-InvokeInternal], ParameterBindingException
FullyQualifiedErrorId : NamedParameterNotFound,Should-InvokeInternal
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Should-Invoke, C:\Users\me\Documents\PowerShell\Modules\Pester\5.3.1\Pester.psm1: line 14898
                        at Invoke-Assertion, C:\Users\me\Documents\PowerShell\Modules\Pester\5.3.1\Pester.psm1: line 8070
                        at Should<End>, C:\Users\me\Documents\PowerShell\Modules\Pester\5.3.1\Pester.psm1: line 8020

Describe your environment

Pester version     : 5.3.1 C:\Users\danthom\Documents\PowerShell\Modules\Pester\5.3.1\Pester.psm1
PowerShell version : 7.2.1
OS version         : Microsoft Windows NT 10.0.22523.0

Steps to reproduce

BeforeAll {
    function MyFunc { }
    Mock MyFunc { }
}
Describe 'Repro' {
    It 'should work' {
        MyFunc
        Should -Invoke -CommandName 'MyFunc' -Exactly 1 -Because 'I called it'
    }
}

Expected Behavior

Test should pass.

Current Behavior

ParameterBindingException: A parameter cannot be found that matches parameter name 'Because'.

Possible Solution? (optional)

My guess is the parameter just needs to be plumbed through.

@fflaten
Copy link
Collaborator

fflaten commented Dec 31, 2021

Thanks for the report!

Feel free to submit a PR if you'd like to contribute the fix. Should be enough to add the parameter in the internal-function and include it in the message template somehow.

@fflaten
Copy link
Collaborator

fflaten commented Dec 31, 2021

We should also consider to add Because to InvokeVerifiable (+ Internal) considering we do list the parameter as a good practice for assertions. Even though the error-messages are already long and complex in that assertion. 😬

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 a pull request may close this issue.

2 participants