2
votes

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 ?

1
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

1 Answers

1
votes

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
}