Skip to main content
Version: v5

New-MockObject

SYNOPSIS

This function instantiates a .NET object from a type.

SYNTAX

Type (Default)

New-MockObject [-Type] <Type> [-Properties <Hashtable>] [-Methods <Hashtable>] [-MethodHistoryPrefix <String>]
[<CommonParameters>]

InputObject

New-MockObject -InputObject <Object> [-Properties <Hashtable>] [-Methods <Hashtable>]
[-MethodHistoryPrefix <String>] [<CommonParameters>]

DESCRIPTION

Using the New-MockObject you can mock an object based on .NET type.

An .NET assembly for the particular type must be available in the system and loaded.

EXAMPLES

EXAMPLE 1

$obj = New-MockObject -Type 'System.Diagnostics.Process'
$obj.GetType().FullName
System.Diagnostics.Process

EXAMPLE 2

$obj = New-MockObject -Type 'System.Diagnostics.Process' -Properties @{ Id = 123 }

EXAMPLE 3

$obj = New-MockObject -Type 'System.Diagnostics.Process' -Methods @{ Kill = { param($entireProcessTree) "killed" } }
$obj.Kill()
$obj.Kill($true)
$obj.Kill($false)

$obj._Kill

Call Arguments
---- ---------
1 {}
2 {True}
3 {False}

PARAMETERS

-Type

The .NET type to create. This creates the object without calling any of its constructors or initializers. Use this to instantiate an object that does not have a public constructor. If your object has a constructor, or is giving you errors, try using the constructor and provide the object using the InputObject parameter to decorate it.

Type: Type
Parameter Sets: Type
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-InputObject

An already constructed object to decorate. Use New-Object or ::new to create it.

Type: Object
Parameter Sets: InputObject
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Properties

Properties to define, specified as a hashtable, in format @{ PropertyName = value }.

Type: Hashtable
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Methods

Methods to define, specified as a hashtable, in format @{ MethodName = scriptBlock }.

ScriptBlock can define param block, and it will receive arguments that were provided to the function call based on order.

Method overloads are not supported because ScriptMethods are used to decorate the object, and ScriptMethods do not support method overloads.

For each method a property named _MethodName is defined which holds history of the invocations of the method and the arguments that were provided.

Type: Hashtable
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-MethodHistoryPrefix

{{ Fill MethodHistoryPrefix Description }}

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: _
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

NOTES

https://pester.dev/docs/commands/New-MockObject

https://pester.dev/docs/usage/mocking

EDIT THIS PAGE

This page was auto-generated using the comment based help in Pester 5.3.3. To edit the content of this page, change the corresponding help in the pester/Pester repository. See our contribution guide for more information.