Thanks to visit codestin.com
Credit goes to github.com

Skip to content

"Should -HaveParameter 'param' -Not -Mandatory" passes tests if parameter is not present #2105

@enoorden

Description

@enoorden

General summary of the issue

"Should -HaveParameter 'param' -Not -Mandatory" passes tests if parameter is not present

Describe your environment

Pester version : 5.3.1 C:\Users___\Documents\PowerShell\Modules\pester\5.3.1\Pester.psm1
PowerShell version : 7.2.0
OS version : Microsoft Windows NT 10.0.19043.0

Steps to reproduce

function hi {
    param(
        [parameter(mandatory)]
        [string]$name
    )
    "hi $name"
}

Context 'ctx' {
    It 'test missing param' {
        $fnc = Get-Command 'hi'
        $fnc | Should -HaveParameter 'anotherParam' -Not -Mandatory
    }
}
Context ctx
  [+] test missing param 6ms (3ms|3ms)

Expected Behavior

would expect the test to fail.

I can see how a parameter which is not declared is actually not mandatory. But i would still expect the 'Should -HaveParameter' to first check the actual presence of the parameter.

Current Behavior

test succeeds

Possible Solution? (optional)

use two tests

Context 'ctx' {
    It 'test missing param' {
        $fnc = Get-Command 'hi'
        $fnc | Should -HaveParameter 'anotherParam'
        $fnc | Should -HaveParameter 'anotherParam' -Not -Mandatory
    }
}
 [-] test missing param 17ms (8ms|9ms)
   Expected command hi to have a parameter anotherParam, but the parameter is missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions