Skip to content

Commit

Permalink
Merge pull request pester#203 from pester/MockingDynamicParameters
Browse files Browse the repository at this point in the history
Mocking commands that have Dynamic Parameters
  • Loading branch information
dlwyatt committed Aug 31, 2014
2 parents 913b4a5 + f5da928 commit db04cb9
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 30 deletions.
133 changes: 133 additions & 0 deletions Functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,136 @@ Describe 'Dot Source Test' {
Assert-MockCalled Test-Path -Exactly 0 -ParameterFilter { $Path -ne 'Test' }
}
}

Describe 'Mocking Cmdlets with dynamic parameters' {
$mockWith = { if (-not $CodeSigningCert) { throw 'CodeSigningCert variable not found, or set to false!' } }
Mock Get-ChildItem -MockWith $mockWith -ParameterFilter { [bool]$CodeSigningCert }

It 'Allows calls to be made with dynamic parameters (including parameter filters)' {
{ Get-ChildItem -Path Cert:\ -CodeSigningCert } | Should Not Throw
Assert-MockCalled Get-ChildItem
}
}

Describe 'Mocking functions with dynamic parameters' {

# Get-Greeting sample function borrowed and modified from Bartek Bielawski's
# blog at http://becomelotr.wordpress.com/2012/05/10/using-and-abusing-dynamic-parameters/

function Get-Greeting {
[CmdletBinding()]
param (
$Name
)

DynamicParam {
if ($Name -cmatch '\b[a-z]') {
$Attributes = New-Object Management.Automation.ParameterAttribute
$Attributes.ParameterSetName = "__AllParameterSets"
$Attributes.Mandatory = $false

$AttributeCollection = New-Object Collections.ObjectModel.Collection[Attribute]
$AttributeCollection.Add($Attributes)

$Dynamic = New-Object System.Management.Automation.RuntimeDefinedParameter('Capitalize', [switch], $AttributeCollection)

$ParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$ParamDictionary.Add("Capitalize", $Dynamic)
$ParamDictionary
}
}

end
{
if($PSBoundParameters.Capitalize) {
$Name = [regex]::Replace(
$Name,
'\b\w',
{ $args[0].Value.ToUpper() }
)
}

"Welcome $Name!"
}
}

$mockWith = { if (-not $Capitalize) { throw 'Capitalize variable not found, or set to false!' } }
Mock Get-Greeting -MockWith $mockWith -ParameterFilter { [bool]$Capitalize }

It 'Allows calls to be made with dynamic parameters (including parameter filters)' {
{ Get-Greeting -Name lowercase -Capitalize } | Should Not Throw
Assert-MockCalled Get-Greeting
}
}

Describe 'Mocking Cmdlets with dynamic parameters in a module' {
New-Module -Name TestModule {
function PublicFunction { Get-ChildItem -Path Cert:\ -CodeSigningCert }
} | Import-Module -Force

$mockWith = { if (-not $CodeSigningCert) { throw 'CodeSigningCert variable not found, or set to false!' } }
Mock Get-ChildItem -MockWith $mockWith -ModuleName TestModule -ParameterFilter { [bool]$CodeSigningCert }

It 'Allows calls to be made with dynamic parameters (including parameter filters)' {
{ TestModule\PublicFunction } | Should Not Throw
Assert-MockCalled Get-ChildItem -ModuleName TestModule
}

Remove-Module TestModule -Force
}

Describe 'Mocking functions with dynamic parameters in a module' {
New-Module -Name TestModule {
function PublicFunction { Get-Greeting -Name lowercase -Capitalize }

# Get-Greeting sample function borrowed and modified from Bartek Bielawski's
# blog at http://becomelotr.wordpress.com/2012/05/10/using-and-abusing-dynamic-parameters/

function Get-Greeting {
[CmdletBinding()]
param (
$Name
)

DynamicParam {
if ($Name -cmatch '\b[a-z]') {
$Attributes = New-Object Management.Automation.ParameterAttribute
$Attributes.ParameterSetName = "__AllParameterSets"
$Attributes.Mandatory = $false

$AttributeCollection = New-Object Collections.ObjectModel.Collection[Attribute]
$AttributeCollection.Add($Attributes)

$Dynamic = New-Object System.Management.Automation.RuntimeDefinedParameter('Capitalize', [switch], $AttributeCollection)

$ParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$ParamDictionary.Add("Capitalize", $Dynamic)
$ParamDictionary
}
}

end
{
if($PSBoundParameters.Capitalize) {
$Name = [regex]::Replace(
$Name,
'\b\w',
{ $args[0].Value.ToUpper() }
)
}

"Welcome $Name!"
}
}
} | Import-Module -Force

$mockWith = { if (-not $Capitalize) { throw 'Capitalize variable not found, or set to false!' } }
Mock Get-Greeting -MockWith $mockWith -ModuleName TestModule -ParameterFilter { [bool]$Capitalize }

It 'Allows calls to be made with dynamic parameters (including parameter filters)' {
{ TestModule\PublicFunction } | Should Not Throw
Assert-MockCalled Get-Greeting -ModuleName TestModule
}

Remove-Module TestModule -Force
}
Loading

0 comments on commit db04cb9

Please sign in to comment.