Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active October 14, 2024 15:13
Show Gist options
  • Save markwragg/7afe4615aef97a9fed80441ffcb0e646 to your computer and use it in GitHub Desktop.
Save markwragg/7afe4615aef97a9fed80441ffcb0e646 to your computer and use it in GitHub Desktop.
Redirect Out-File to write to TestDrive: during Pester execution
BeforeAll {
$OutFile = Get-Command -Name 'Out-File'
Mock Out-File -ParameterFilter { $FilePath -match 'file.txt' } -MockWith {
$InputObject = $PesterBoundParameters['InputObject']
& $OutFile -InputObject $InputObject -FilePath (Join-Path $TestDrive 'file.txt')
}
}
It 'Should update the file with the expected values' {
$FilePath = (Join-Path $TestDrive 'file.txt')
$FilePath | Should -FileContentMatch 'expectedstring1'
$FilePath | Should -FileContentMatch 'expectedstring2'
$FilePath | Should -FileContentMatch 'expectedstring3'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment