Logo

dev-resources.site

for different kinds of informations.

Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick

Published at
10/19/2024
Categories
powershell
pester
devops
testing
Author
markwragg
Categories
4 categories in total
powershell
open
pester
open
devops
open
testing
open
Author
9 person written this
markwragg
open
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick

I was writing some Pester unit tests recently for a PowerShell script that creates a modified version of a template file before it is then uploaded via an API and deleted. The script uses Out-File to output the updated file and because it only exists temporarily the location it writes to is hard coded in the script.

I wanted to write some tests that would validate the file was created and with the expected updated values. I could have just let the script write the file to it's default location and validate it there, but I'd have to mock Remove-Item to stop it deleting the file and then have my test script remove it afterwards. Another approach might have been to do some refactoring, have the output file and input parameter of the script, or break the script up into smaller functions, but I didn't particularly want to change the script.

Pester provides an automatic location TestDrive: that can be used to isolate file operations somewhere unique and that it automatically cleans up when the test completes, so all I needed to do was override the behaviour of Out-File to write to TestDrive:. You can do that with a Mock, and the one-weird-trick here is to store the Out-File command into a variable so that we can use it within the Mock, but change it's input parameters:

BeforeAll {
  $OutFile = Get-Command -Name 'Out-File'

  Mock Out-File -ParameterFilter { $FilePath -match 'file.txt' } -MockWith {
    $InputObject = $PesterBoundParameters['InputObject']
    $FilePath = Join-Path $TestDrive 'file.txt'
    & $OutFile -InputObject $InputObject -FilePath $FilePath
  }
}

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'
}
Enter fullscreen mode Exit fullscreen mode

It is necessary to put the command into a variable because once a command is mocked in a test script, calling it would invoke the Mock rather than the command, so you'd end up in a recursive loop. Note how the Mock uses $PesterBoundParameters to access the parameters that were passed to the command in the script, but we then override what would have been provided for -FilePath with our TestDrive: pathed value.

My script still mocked Remove-Item btw, so we could check that it had been called the expected number of times. There was no need to let it actually do a file removal because the clean up of TestDrive: takes care of that for us. It's usually best to mock away the destructive behaviour of cmdlets when testing so that if the script has been badly modified in some way you don't risk invoking the actual behaviour for the purpose of running your tests.

powershell Article's
30 articles in total
Favicon
Windows ไธŠ VSCode ็š„ C/C++ ๅปถไผธๆจก็ต„่™•็†็ทจ็ขผ็š„ๅ•้กŒ
Favicon
PowerShell ็š„ๆ–‡ๅญ—็ทจ็ขผ
Favicon
็ฌ”่ฎฐ3
Favicon
TryHackMe | Windows PowerShell | RSCyberTech
Favicon
Configuring Hyper-V Global Default Directories
Favicon
How I Set Up an Awesome PowerShell Environment for Script Development
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
File Comparison Made Easy: Detecting New and Changed Files with PowerShell
Favicon
10 Best Practices of PowerShell Code Signing for Signing Your Script
Favicon
PowerShell Script Collection: Automation and Solutions for Everyday Tasks
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
Set up Azure Network Security Perimeter with PowerShell
Favicon
Automating Azure Project Setup with PowerShell and GitHub Actions
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Using PowerShell to Create Service Principals for RBAC
Favicon
Two ways to use Pester to Mock objects with strongly typed parameters
Favicon
PowerShell Automation: Execute Batch GitHub CLI Commands
Favicon
โ“ Do you allow wrong input to enter your function?
Favicon
Introducing PowerShell Utility Scripts
Favicon
Cisco DHCP Pool conversion to Windows Server DHCP
Favicon
PowerShell | Script output garbled Chinese characters
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! ๐Ÿš€
Favicon
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick
Favicon
Using PowerShell for day to day stuff
Favicon
Automating SharePoint Embedded: Using PowerShell to Call Graph API Endpoints
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
How to Run PowerShell Script in Jenkins Pipeline
Favicon
Identify Which Files are Large.
Favicon
Quick guide to setting up an shortcut/alias in windows

Featured ones: