orange-lily
orange-lily

Reputation: 221

Can Pester Mock [Type]::new()

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 ?

Upvotes: 2

Views: 339

Answers (1)

Efie
Efie

Reputation: 1690

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
}

Upvotes: 1

Related Questions