Skip to content

Allow use of parameter aliases in Mock -ParameterFilter #1128

@richardnagle

Description

@richardnagle

1. General summary of the issue

Powershell parameters can be defined with aliases:

function Get-Something{    
   param(
        [Alias('Path')]
        [string] $PathName)
   #function body
}

Get-Something -Path c:\temp.txt

When mocking with -ParameterFilter you cannot currently use the alias name to filter, but must use the real parameter name.

Mock Get-Something -ParameterFilter {$PathName = "c:\temp.txt"} {return 123} #works
Mock Get-Something -ParameterFilter {$Path = "c:\temp.txt"} {return 456} #fails

2. Describe Your Environment

Pester version : 4.4.0 3.4.0 3.3.5 C:\Program Files\WindowsPowerShell\Modules\Pester\4.4.0\Pester.psd1 C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psd1 C:\Program Files\WindowsPowerShell\Modules\Pester\3.3.5\Pester.psd1
PowerShell version : 5.1.15063.1292
OS version : Microsoft Windows NT 10.0.15063.0

3. Expected Behavior

Get-Content has a parameter named -Tail which has an alias -Last

(Get-Command get-content).ParameterSets[0] | select -ExpandProperty parameters | select Name,Aliases | where Name -eq Tail

Name Aliases
---- -------
Tail {Last}

Therefore I would expect this test should pass:

It "Uses parameter aliases in Parameter-Filter" {
   Mock Get-Content {return "default-get-content"}
   Mock Get-Content -ParameterFilter {$Last -eq 100} -MockWith {return "aliased-parameter-name"}

   Get-Content -Path "c:\temp.txt" -Last 100 | Should -Be "aliased-parameter-name"
}                                       

4.Current Behavior

When using an alias in -ParameterFilter the variable never has a value.

The output from the above test is

[-] Uses parameter aliases in Parameter-Filter 50ms         
  Expected strings to be the same, but they were different. 
  Expected length: 22                                       
  Actual length:   19                                       
  Strings differ at index 0.                                
  Expected: 'aliased-parameter-name'                        
  But was:  'default-get-content'                           
  -----------^    

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions