Author Posts

April 30, 2018 at 5:18 pm

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

April 30, 2018 at 8:43 pm

Hi All,

I figured out mistake. It was related to what I was passing into my ParameterFilter scriptblock and what I was actually passing into the function. They didn't match so the function was not being mocked as I thought it was.

Thanks,
Michael