Skip to content

Keep New-MockObject properties when the mock is passed through Select-Object - #3093

Open
dirtyvanity wants to merge 4 commits into
pester:mainfrom
dirtyvanity:fix-2393-mockobject-select-object
Open

dirtyvanity wants to merge 4 commits into
pester:mainfrom
dirtyvanity:fix-2393-mockobject-select-object

Conversation

@dirtyvanity

@dirtyvanity dirtyvanity commented Dec 1, 2026 •

Copy link
Copy Markdown

PR Summary

New-MockObject -Properties added its note properties with $mock.PSObject.Properties.Add(). Those members are lost when the mock is a property value expanded with Select-Object -ExpandProperty, so the issue's nested case returned $null:

$mock = New-MockObject -Type object -Properties @{
    Nested = New-MockObject -Type object -Properties @{ Name = 'Value' }
}
$mock.Nested.Name                                                                  # 'Value'
$mock | Select-Object -ExpandProperty Nested | Select-Object -ExpandProperty Name  # was $null, now 'Value'

The cause: each value went through [Pester.Factory]::CreateNoteProperty(), and passing a value to a .NET method unwraps its PSObject. Select-Object -ExpandProperty then wraps the bare value with PSObject.AsPSObject(value, storeTypeNameAndInstanceMembersLocally: true) (Select-Object.cs), and that new wrapper doesn't see members added to the object earlier.

As suggested in the issue, this now uses Add-Member (via $SafeCommands). As fflaten found, Add-Member -Force alone isn't enough: the mock has to be the object -PassThru returns, so it's kept as a PSObject. All properties are added in one call with -NotePropertyMembers, with -ErrorAction Stop so a property that can't be added (for example Process.Name, an alias from the type data) still fails the test as before. A mock with only -Methods is wrapped too, so a nested one keeps its methods.

Speed (pwsh 7.6, Linux, per call, two runs): about the same with 1 property (27–43 µs before, 29–36 µs after), the same or faster with 10, and faster with 50 (115–130 µs before, 69–84 µs after).

Fix #2393

PR Checklist

  • PR has meaningful title
  • Summary describes changes
  • PR is ready to be merged
  • Tests are added/update (if required)
  • Documentation is updated/added (if required): not required, no behaviour change beyond the fix

Tests added to tst/functions/New-MockObject.Tests.ps1: a nested mock's property, nested mocks in an array, and a methods-only nested mock through Select-Object -ExpandProperty (these three fail on main); a property that can't be added throws; an empty -Properties @{}; replacing an existing property; a typed mock keeps its type and methods.

Tested on PowerShell 7.4 and 7.6 (Linux). I couldn't run Windows PowerShell 5.1 locally.

🤖

Properties added with $mock.PSObject.Properties.Add() were lost when the
mock was a property value expanded with Select-Object -ExpandProperty.
Add them with Add-Member -NotePropertyMembers -PassThru and keep the
returned object instead. One call adds all properties, so it is no slower.

🤖
The old code passed each value through a .NET method, which unwraps its
PSObject. Select-Object -ExpandProperty then wraps the bare value in a new
PSObject that stores members locally and so doesn't see the ones added
earlier.

🤖
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@d59a2c9). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3093   +/-   ##
=======================================
  Coverage        ?   91.84%           
=======================================
  Files           ?        2           
  Lines           ?     8656           
  Branches        ?        0           
=======================================
  Hits            ?     7950           
  Misses          ?      706           
  Partials        ?        0           
Flag Coverage Δ
ps51-windows-2022 91.31% <ø> (?)
ps51-windows-latest 91.31% <ø> (?)
ps7-macos-15 90.74% <ø> (?)
ps7-macos-latest 90.74% <ø> (?)
ps7-ubuntu-2204 90.75% <ø> (?)
ps7-ubuntu-latest 90.75% <ø> (?)
ps7-windows-2022 91.40% <ø> (?)
ps7-windows-latest 91.40% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add-Member reports a failed add as a non-terminating error, so a property
that can't be added (for example Process.Name, an alias from the type data)
made New-MockObject return $null instead of failing the test as before.
-ErrorAction Stop restores the failure.

A nested mock with only -Methods still lost its methods through
Select-Object -ExpandProperty, as it did before this change; the mock is now
returned wrapped either way.

Tests: a failed add throws; nested mocks in an array and a methods-only
nested mock survive expansion; the first test is renamed, since it passed
before too.

🤖
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Select-Object and New-MockObject - Discarded PSObject

1 participant