2

I have script that has the following code:

 $var1 = [TESTType]::new($Var)

I would like to run a pester test that Mock [TESTType]::new($Var). Is This possible ?

user7786267
  • 137
  • 12
  • `Mock` won't be able to, but in PowerShell >6 you could add a `class TESTType` definition in the test fixture and it should take precedence over any existing custom classes – Mathias R. Jessen Sep 09 '20 at 15:12

1 Answers1

1

One option is to use a wrapper function. You could for example do this:

function Get-TESTType {
    param(
    [Parameter()]$ContructorVar
    )
    return [TESTType]::new($ConstructorVar)
}

Then you can mock this function

It "Mocks the TESTType Class" {
    Mock Get-TESTType {
        @{
            field1 = 'something'
            field2 = 'something'
        }
    }
    # Put your test here
}
Efie
  • 1,430
  • 2
  • 14
  • 34
  • Just happened to stumble across this while looking for something else. Might be worth looking into instead: https://pester-docs.netlify.app/docs/commands/New-MockObject – Efie Oct 21 '20 at 15:17