forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This works, but there might be some weirdness that can still happen if you mock a global function in conjunction with -ModuleName or InModuleScope. (The global function gets renamed for the duration of the mock, but the mock is only available from that single module. You can't mock the same global function from two different modules at once.) Not sure if it's worth worrying about that edge case, since this "mocking global functions" scenario is already pretty rare to begin with.
- Loading branch information
Showing
3 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This script exists to create and mock a global function, then exit. The actual behavior | ||
# that we need to test is covered in GlobalMock-B.Tests.ps1, where we make sure that the | ||
# global function was properly restored in its scope. | ||
|
||
$functionName = '01c1a57716fe4005ac1a7bf216f38ad0' | ||
|
||
if (Test-Path Function:\$functionName) | ||
{ | ||
Remove-Item Function:\$functionName -Force -ErrorAction Stop | ||
} | ||
|
||
function global:01c1a57716fe4005ac1a7bf216f38ad0 | ||
{ | ||
return 'Original Function' | ||
} | ||
|
||
function script:Testing | ||
{ | ||
return 'Script scope' | ||
} | ||
|
||
Describe 'Mocking Global Functions - Part One' { | ||
Mock $functionName { | ||
return 'Mocked' | ||
} | ||
|
||
It 'Mocks the global function' { | ||
& $functionName | Should Be 'Mocked' | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This test depends on some state set up in GlobalMock-A.Tests.ps1. The behavior we're verifying | ||
# is that global functions that have been mocked are still properly set up even after the test | ||
# script exits its scope. | ||
|
||
$functionName = '01c1a57716fe4005ac1a7bf216f38ad0' | ||
|
||
try | ||
{ | ||
Describe 'Mocking Global Functions - Part Two' { | ||
It 'Restored the global function properly' { | ||
$globalFunctionExists = Test-Path Function:\global:$functionName | ||
$globalFunctionExists | Should Be $true | ||
& $functionName | Should Be 'Original Function' | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
if (Test-Path Function:\$functionName) | ||
{ | ||
Remove-Item Function:\$functionName -Force | ||
} | ||
} |
This file contains 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