Skip to content

Instantly share code, notes, and snippets.

@johlju
Created May 13, 2019 19:19
Show Gist options
  • Save johlju/3562d41966157f1d853d3df0280abd9c to your computer and use it in GitHub Desktop.
Save johlju/3562d41966157f1d853d3df0280abd9c to your computer and use it in GitHub Desktop.
Mock using Closure
Describe "a" {
It "b" {
function a () {}
$closure = & {
$state = @{
Value = 0
}
{
if ($state.Value -eq 0)
{
# This is first call
$mockReturnValue = @{
Ensure = 'Absent'
}
}
else
{
# This is subsequent call
$mockReturnValue = @{
Ensure = 'Present'
}
}
$state.Value++
return $mockReturnValue
}.GetNewClosure()
}
Mock a -MockWith $closure
$result = a
$result.Ensure | Should -Be 'Absent'
$result = a
$result.Ensure | Should -Be 'Present'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment