From 25e603a9aa2f51b0217c0eb8a662a6810c42b8d0 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Fri, 17 Jan 2025 20:16:07 +0000 Subject: [PATCH 1/4] Add New-MockObject example and tests for internal class --- src/functions/New-MockObject.ps1 | 14 ++++++++++++++ tst/functions/New-MockObject.Tests.ps1 | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/functions/New-MockObject.ps1 b/src/functions/New-MockObject.ps1 index 26f088de6..95d56b203 100644 --- a/src/functions/New-MockObject.ps1 +++ b/src/functions/New-MockObject.ps1 @@ -32,6 +32,10 @@ .EXAMPLE ```powershell $obj = New-MockObject -Type 'System.Diagnostics.Process' + $obj.GetType().FullName + System.Diagnostics.Process + + $obj = New-MockObject -Type ([System.Diagnostics.Process]) $obj.GetType().FullName System.Diagnostics.Process ``` @@ -64,6 +68,16 @@ Create a mock of a process-object and mocks the object's `Kill()`-method. The mocked method will keep a history of any call and the associated arguments in a property named `_Kill` + .EXAMPLE + ```powershell + $someObj = Get-ObjectFromModule + $mock = New-MockObject -Type $someObj.GetType() + $mock.GetType().FullName + .MyInternalClass + ``` + + Create a mock by providing a TypeInfo, e.g. to mock output using an internal module class. + .LINK https://pester.dev/docs/commands/New-MockObject diff --git a/tst/functions/New-MockObject.Tests.ps1 b/tst/functions/New-MockObject.Tests.ps1 index 81981b9ad..0ce177732 100644 --- a/tst/functions/New-MockObject.Tests.ps1 +++ b/tst/functions/New-MockObject.Tests.ps1 @@ -35,6 +35,28 @@ Describe 'New-MockObject' { { New-MockObject $type } | Should -Not -Throw } + It 'Mock using type input' { + New-MockObject -Type ([System.Diagnostics.Process]) | Should -BeOfType ([System.Diagnostics.Process]) + } + + It 'Mock internal classes using type' { + # Simulate a internal module class like https://github.com/pester/Pester/issues/2564 + $someObj = & { + class MyInternalClass { + [string] $Name + [string] GetName() { return $this.Name } + } + $obj = [MyInternalClass]::new(); + $obj.Name = 'Real' + $obj + } + + { [MyInternalClass] } | Should -Throw -ErrorId 'TypeNotFound' + $mock = New-MockObject -Type $someObj.GetType() -Properties @{ Name = 'Mocked' } + $mock.GetType().Name | Should -Be 'MyInternalClass' + $mock.GetName() | Should -Be 'Mocked' + } + Context 'Methods' { It "Adds a method to the object" { $o = New-Object -TypeName 'System.Diagnostics.Process' From 8bba3c982c78073f7385bb3cf72370e7652e2599 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Fri, 17 Jan 2025 20:28:05 +0000 Subject: [PATCH 2/4] Wrap test in version check for easier backport --- tst/functions/New-MockObject.Tests.ps1 | 30 ++++++++++++++------------ 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tst/functions/New-MockObject.Tests.ps1 b/tst/functions/New-MockObject.Tests.ps1 index 0ce177732..3df6d04c3 100644 --- a/tst/functions/New-MockObject.Tests.ps1 +++ b/tst/functions/New-MockObject.Tests.ps1 @@ -39,22 +39,24 @@ Describe 'New-MockObject' { New-MockObject -Type ([System.Diagnostics.Process]) | Should -BeOfType ([System.Diagnostics.Process]) } - It 'Mock internal classes using type' { - # Simulate a internal module class like https://github.com/pester/Pester/issues/2564 - $someObj = & { - class MyInternalClass { - [string] $Name - [string] GetName() { return $this.Name } + if ($PSVersionTable.PSVersion.Major -ge 5) { + It 'Mock internal classes using type' { + # Simulate a internal module class like https://github.com/pester/Pester/issues/2564 + $someObj = & { + class MyInternalClass { + [string] $Name + [string] GetName() { return $this.Name } + } + $obj = [MyInternalClass]::new() + $obj.Name = 'Real' + $obj } - $obj = [MyInternalClass]::new(); - $obj.Name = 'Real' - $obj - } - { [MyInternalClass] } | Should -Throw -ErrorId 'TypeNotFound' - $mock = New-MockObject -Type $someObj.GetType() -Properties @{ Name = 'Mocked' } - $mock.GetType().Name | Should -Be 'MyInternalClass' - $mock.GetName() | Should -Be 'Mocked' + { [MyInternalClass] } | Should -Throw -ErrorId 'TypeNotFound' + $mock = New-MockObject -Type $someObj.GetType() -Properties @{ Name = 'Mocked' } + $mock.GetType().Name | Should -Be 'MyInternalClass' + $mock.GetName() | Should -Be 'Mocked' + } } Context 'Methods' { From 699e6f89c0e36d2dc79c4b8d78f94883f1e4f521 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Fri, 17 Jan 2025 21:18:23 +0000 Subject: [PATCH 3/4] fix something please --- tst/functions/New-MockObject.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tst/functions/New-MockObject.Tests.ps1 b/tst/functions/New-MockObject.Tests.ps1 index 3df6d04c3..52d27b961 100644 --- a/tst/functions/New-MockObject.Tests.ps1 +++ b/tst/functions/New-MockObject.Tests.ps1 @@ -44,7 +44,7 @@ Describe 'New-MockObject' { # Simulate a internal module class like https://github.com/pester/Pester/issues/2564 $someObj = & { class MyInternalClass { - [string] $Name + [string] $Name = 'Default' [string] GetName() { return $this.Name } } $obj = [MyInternalClass]::new() From 2d84af0fa25f1b800da8a15cbcff3f09d3c87481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Fri, 17 Jan 2025 22:27:38 +0100 Subject: [PATCH 4/4] Add default ctors to make code run under Profiler --- tst/functions/New-MockObject.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tst/functions/New-MockObject.Tests.ps1 b/tst/functions/New-MockObject.Tests.ps1 index 52d27b961..bf3c9eb8d 100644 --- a/tst/functions/New-MockObject.Tests.ps1 +++ b/tst/functions/New-MockObject.Tests.ps1 @@ -44,6 +44,8 @@ Describe 'New-MockObject' { # Simulate a internal module class like https://github.com/pester/Pester/issues/2564 $someObj = & { class MyInternalClass { + MyInternalClass() { } + [string] $Name = 'Default' [string] GetName() { return $this.Name } }