Skip to content

Commit

Permalink
Add New-MockObject function and export it from Pester
Browse files Browse the repository at this point in the history
  • Loading branch information
adbertram authored and nohwnd committed Nov 12, 2016
1 parent 67e8bfc commit b81acba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Functions/New-MockObject.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe 'New-MockObject' {

it 'instantiates an object from a class with no public constructors' {
$type = 'Microsoft.PowerShell.Commands.Language'
New-MockObject -Type $type | should beoftype $type
}
}
24 changes: 24 additions & 0 deletions Functions/New-MockObject.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function New-MockObject {
<#
.SYNOPSIS
This function instantiates a .NET object from a type. The assembly for the particular type must be
loaded.
.PARAMETER Type
The .NET type to create an object from.
.EXAMPLE
PS> $obj = New-MockObject -Type 'System.Diagnostics.Process'
PS> $obj.GetType().FullName
System.Diagnostics.Process
#>

param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[type]$Type
)

[System.Runtime.Serialization.Formatterservices]::GetUninitializedObject($Type)

}
3 changes: 2 additions & 1 deletion Pester.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ FunctionsToExport = @(
'Set-DynamicParameterVariables',
'Set-TestInconclusive',
'SafeGetCommand',
'New-PesterOption'
'New-PesterOption',
'New-MockObject'
)

# # Cmdlets to export from this module
Expand Down
1 change: 1 addition & 0 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,4 @@ if ((& $script:SafeCommands['Test-Path'] -Path Variable:\psise) -and
& $script:SafeCommands['Export-ModuleMember'] BeforeEach, AfterEach, BeforeAll, AfterAll
& $script:SafeCommands['Export-ModuleMember'] Get-MockDynamicParameters, Set-DynamicParameterVariables
& $script:SafeCommands['Export-ModuleMember'] SafeGetCommand, New-PesterOption
& $script:SafeCommands['Export-ModuleMember'] New-MockObject

0 comments on commit b81acba

Please sign in to comment.