Mocked Object Properties

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Monte Hazboun Monte Hazboun 2 weeks, 4 days ago.

  • Author
    Posts
  • #68262
    Profile photo of Monte Hazboun
    Monte Hazboun
    Participant

    Hello Everyone,

    Currently when using New-MockObject we get an empty object with no properties set. It seems these properties are read only and aren't able to be set without overriding the entire property with a note property of the same name. Is this the preferred method for populating the mock object, or is there another way to set these properties that I may be missing?

    For reference here is my current code to override the properties:

        $VM = Get-Content .\VirtualMachine.tests.json -Raw | ConvertFrom-Json
        $VMMock = New-MockObject -Type 'Microsoft.Hyperv.PowerShell.VirtualMachine'
        foreach ($property in ($VM | Get-Member -MemberType NoteProperty).Name) { 
            $addmembersplat = @{
                MemberType = [System.Management.Automation.PSMemberTypes]::NoteProperty
                Name = $property
                value = $vm.$property
                Force = $true   
        }
    
        $VMMock | Add-Member @addmembersplat -ErrorAction 0
        }
    
  • #68266
    Profile photo of Adam Bertram
    Adam Bertram
    Moderator

    Yes. That currently is the only way to populate the properties. I've intended to add that functionality to the New-MockObject function for awhile now but just haven't got to it it. I want to do something like this:

    New-MockObject -Type 'FOO' -Property @{Name='bar';Type = 'Property'; Value='foo'},@{Name = 'method1';Type = 'Method'; Value = 'x'}

You must be logged in to reply to this topic.