Skip to content

Write-Host does not implicitly resolve a MockObject #2698

@PowerCoder

Description

@PowerCoder

Checklist

What is the issue?

I have this line of code:

Write-Host "Deploying : $Package"

This should output a filename which it got from Get-ChildItem.

Since no property is provided to Write-Host it defaults to use a .ToString() which normally outputs FullName

I want to test it using ParameterFilter so my test includes { $Object -eq ('Deploying : {0}' -f $Package.FullName).

To avoid using filesystem actions I figured I would use a MockObject:

$Package = New-MockObject -Type 'System.IO.FileInfo' -Properties @{
  'Name' = 'Deploy.zip'
  'FullName' = 'Folder\Deploy.zip'
} -Methods @{
  'ToString' = { Return 'Folder\Deploy.zip' }
}
Mock -CommandName 'Get-ChildItem' -MockWith { Return $WebDeployPackage }

If I do $Package.ToString() it will correctly return the output.

If I do Write-Host $Package it returns a blank line.

Simply using Write-Host $Package.FullName works fine, it's the implicit transform which fails.

Expected Behavior

According to the documentation it should use the ToString Method. Since I mocked that Method I would expect Write-Host to return the string I set.

Steps To Reproduce

No response

Describe your environment

Pester version : 5.7.1
PowerShell version : 7.4.13
OS version : NT 10.0.22631.0

Possible Solution?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions