Pester Test with Dynamic Variables

This topic contains 1 reply, has 1 voice, and was last updated by  Michael 3 weeks, 3 days ago.

  • Author
    Posts
  • #99993

    Michael
    Participant

    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

  • #100008

    Michael
    Participant

    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

You must be logged in to reply to this topic.