Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Last active September 24, 2019 09:16
Show Gist options
  • Save nohwnd/cdc24a9edbd1c963dc776c083777dc3d to your computer and use it in GitHub Desktop.
Save nohwnd/cdc24a9edbd1c963dc776c083777dc3d to your computer and use it in GitHub Desktop.
Check variable in parent scope, an interesting approach to testing a parent if condition.
Get-Module m | Remove-Module
New-Module -Name m -ScriptBlock {
function Do-Expensive { }
} | Import-Module
function f () {
$variable = 2
# if ($variable -eq 1) {
Do-Expensive
# }
}
Describe "a" {
It "b" {
Mock -CommandName "Do-Expensive" -MockWith {
if ($variable -ne 1) {
throw "Variable `$variable is not 1, it is $($variable)."
}
else {
Write-Host "Variable `$variable is 1."
}
}
f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment