Created
May 13, 2019 19:19
-
-
Save johlju/3562d41966157f1d853d3df0280abd9c to your computer and use it in GitHub Desktop.
Mock using Closure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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