Howdy everyone,
I have a cmdlet within a module I am testing where 3 of the 4 params are dynamically built at runtime. Within the cmdlet, I assign a new variable to the passed in value and use that variable within a custom object.
My issue is when I write the pester test and mock the dynamic param, the object I am returning doesn’t get picked by the $PSboundParameters variable
function Get-Something {
param()
dynamicParam{
Set-DynamicVariable -NameScriptPairs @{Name=ServerFunction;}
}
$value = $PsBoundParameters.ServerFunction
# some code here
...
$Computer = [pscustomObject] @{
ComputerName = $ComputerName
ServerFunction = $value
}
return $Computer
}
My Pester test looks like a little like this:
InModuleScope MyModule {
Describe 'Get-Something' {
# test data
$Env = @{ServerFunction = 'Ingess'}
it 'should return Computer Objects with assigned data' {
Mock Set-DynamicVariable { $Env } -ParameterFilter {$NameScriptPairs -and $NameScriptPairs -match @{Name='ServerFunction'}}
$result = Get-Something
$result.ServerFunction | Should be 'Ingress'
}
}
}
The Pester runs but fails because the expected value is “Ingress” but it was “” (null).
How do I get this to work?
Thanks,
Michael