File tree Expand file tree Collapse file tree 5 files changed +59
-4
lines changed
Expand file tree Collapse file tree 5 files changed +59
-4
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,14 @@ InModuleScope Pester {
2424 function MockMe { param ($Name ) }
2525 Mock MockMe
2626
27+ BeforeEach {
28+ $testState.EnterTest ()
29+ }
30+
31+ AfterEach {
32+ $testState.LeaveTest ()
33+ }
34+
2735 Context ' Handling errors in the Fixture' {
2836 $testState = New-PesterState - Path $TestDrive
2937
Original file line number Diff line number Diff line change @@ -246,6 +246,7 @@ function Invoke-Test
246246 $errorRecord = $null
247247 try
248248 {
249+ $pester.EnterTest ()
249250 Invoke-TestCaseSetupBlocks
250251
251252 do
@@ -271,6 +272,8 @@ function Invoke-Test
271272 {
272273 $errorRecord = $_
273274 }
275+
276+ $pester.LeaveTest ()
274277 }
275278
276279 $result = ConvertTo-PesterResult - ErrorRecord $errorRecord
Original file line number Diff line number Diff line change @@ -1720,6 +1720,31 @@ Describe 'Passing unbound script blocks as mocks' {
17201720 }
17211721}
17221722
1723+ Describe ' Assert-MockCalled when mock called outside of It block' {
1724+ function TestMe { ' Original ' }
1725+ mock TestMe { ' Mocked' }
1726+
1727+ $null = TestMe
1728+
1729+ Context ' Context' {
1730+ $null = TestMe
1731+
1732+ It ' Should log the correct number of calls' {
1733+ TestMe | Should - Be Mocked
1734+ Assert-MockCalled TestMe - Scope It - Exactly - Times 1
1735+ Assert-MockCalled TestMe - Scope Context - Exactly - Times 2
1736+ Assert-MockCalled TestMe - Scope Describe - Exactly - Times 3
1737+ }
1738+
1739+ It ' Should log the correct number of calls (second test)' {
1740+ TestMe | Should - Be Mocked
1741+ Assert-MockCalled TestMe - Scope It - Exactly - Times 1
1742+ Assert-MockCalled TestMe - Scope Context - Exactly - Times 3
1743+ Assert-MockCalled TestMe - Scope Describe - Exactly - Times 4
1744+ }
1745+ }
1746+ }
1747+
17231748Describe " Restoring original commands when mock scopes exit" {
17241749 function a (){}
17251750 Context " first context" {
Original file line number Diff line number Diff line change @@ -1077,8 +1077,8 @@ function ExecuteBlock
10771077
10781078 $Block.Verifiable = $false
10791079
1080- # We set Scope to $null here to indicate the call came from the current Test Case. It'll get assigned to a test group scope when Exit-MockScope is called.
1081- $Mock.CallHistory += @ {CommandName = " $ModuleName ||$CommandName " ; BoundParams = $BoundParameters ; Args = $ArgumentList ; Scope = $null }
1080+ $scope = if ( $pester .InTest ) { $null } else { $pester .CurrentTestGroup }
1081+ $Mock.CallHistory += @ {CommandName = " $ModuleName ||$CommandName " ; BoundParams = $BoundParameters ; Args = $ArgumentList ; Scope = $scope }
10821082
10831083 $scriptBlock = {
10841084 param (
Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ function New-PesterState
5050 $script :CommandCoverage = @ ()
5151 $script :Strict = $Strict
5252 $script :Show = $Show
53+ $script :InTest = $false
5354
5455 $script :TestResult = @ ()
5556
@@ -297,6 +298,21 @@ function New-PesterState
297298 return $script :TestGroupStack.Peek ().AfterAll
298299 }
299300
301+ function EnterTest
302+ {
303+ if ($script :InTest )
304+ {
305+ throw ' You are already in a test case.'
306+ }
307+
308+ $script :InTest = $true
309+ }
310+
311+ function LeaveTest
312+ {
313+ $script :InTest = $false
314+ }
315+
300316 $ExportedVariables = " TagFilter" ,
301317 " ExcludeTagFilter" ,
302318 " TestNameFilter" ,
@@ -315,7 +331,8 @@ function New-PesterState
315331 " IncludeVSCodeMarker" ,
316332 " TestActions" ,
317333 " TestGroupStack" ,
318- " TestSuiteName"
334+ " TestSuiteName" ,
335+ " InTest"
319336
320337 $ExportedFunctions = " EnterTestGroup" ,
321338 " LeaveTestGroup" ,
@@ -324,7 +341,9 @@ function New-PesterState
324341 " GetTestCaseSetupBlocks" ,
325342 " GetTestCaseTeardownBlocks" ,
326343 " GetCurrentTestGroupSetupBlocks" ,
327- " GetCurrentTestGroupTeardownBlocks"
344+ " GetCurrentTestGroupTeardownBlocks" ,
345+ " EnterTest" ,
346+ " LeaveTest"
328347
329348 & $SafeCommands [' Export-ModuleMember' ] - Variable $ExportedVariables - function $ExportedFunctions
330349 } |
You can’t perform that action at this time.
0 commit comments