Skip to content

Instantly share code, notes, and snippets.

@bgelens
Created June 7, 2018 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgelens/903bfa921771ac66e336efd8653f6703 to your computer and use it in GitHub Desktop.
Save bgelens/903bfa921771ac66e336efd8653f6703 to your computer and use it in GitHub Desktop.
Have ErrorVariable populated through a Mock
Mock -CommandName Get-SomeCommand -MockWith {
param (
$ErrorVariable
)
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new('Failed!'),
'Error',
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$null
)
Set-Variable -Name $ErrorVariable -Value $errorRecord -Scope 3
}
@bgelens
Copy link
Author

bgelens commented Jun 7, 2018

This is what I came up with when testing a command that threw a non terminating error which populated ErrorVariable that changed the code flow. By having the mock setup like this, the ErrorVariable passed in is actually set.

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