Skip to content

Instantly share code, notes, and snippets.

@sixdub
Created May 11, 2016 16:10
Show Gist options
  • Save sixdub/10edb2506d8cd268a4c447774ed8d541 to your computer and use it in GitHub Desktop.
Save sixdub/10edb2506d8cd268a4c447774ed8d541 to your computer and use it in GitHub Desktop.
Can you Mock an internal locally scoped function in Pester?
######## SCRIPT EXAMPLE ########
function Do-SomethingStupid
{
function local:Get-NumberFive
{
return 5
}
$value = Get-NumberFive
if (value -ne 5)
{
throw "ERROR"
}
Write-Output "Value $value"
}
######### PESTER TEST #######
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
Describe "Do-SomethingStupid" {
Context 'Wrong value returned' {
#### THIS MOCK WILL NOT WORK. I GET AN ERROR ON VALIDATE-FUNCTION ####
Mock Get-NumberFive {2}
It 'Should not fail due to wrong value' {
{Do-SomethingStupid} | Should Throw
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment