File tree Expand file tree Collapse file tree 4 files changed +43
-9
lines changed
Expand file tree Collapse file tree 4 files changed +43
-9
lines changed 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 @@ -1735,15 +1735,27 @@ Describe 'Passing unbound script blocks as mocks' {
17351735 }
17361736}
17371737
1738- Describe ' Case sensitive? ' {
1738+ Describe ' Assert-MockCalled when mock called outside of It block ' {
17391739 function TestMe { ' Original ' }
17401740 mock TestMe { ' Mocked' }
17411741
17421742 $null = TestMe
17431743
1744- It ' Should work fine' {
1745- TestMe | Should - Be Mocked
1746- Assert-MockCalled TestMe - Scope It - Exactly - Times 1
1747- Assert-MockCalled TestMe - Scope Describe - Exactly - Times 2
1744+ Context ' Context' {
1745+ $null = TestMe
1746+
1747+ It ' Should log the correct number of calls' {
1748+ TestMe | Should - Be Mocked
1749+ Assert-MockCalled TestMe - Scope It - Exactly - Times 1
1750+ Assert-MockCalled TestMe - Scope Context - Exactly - Times 2
1751+ Assert-MockCalled TestMe - Scope Describe - Exactly - Times 3
1752+ }
1753+
1754+ It ' Should log the correct number of calls (second test)' {
1755+ TestMe | Should - Be Mocked
1756+ Assert-MockCalled TestMe - Scope It - Exactly - Times 1
1757+ Assert-MockCalled TestMe - Scope Context - Exactly - Times 3
1758+ Assert-MockCalled TestMe - Scope Describe - Exactly - Times 4
1759+ }
17481760 }
17491761}
Original file line number Diff line number Diff line change @@ -1103,8 +1103,8 @@ function ExecuteBlock
11031103
11041104 $Block.Verifiable = $false
11051105
1106- # 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.
1107- $Mock.CallHistory += @ {CommandName = " $ModuleName ||$CommandName " ; BoundParams = $BoundParameters ; Args = $ArgumentList ; Scope = $null }
1106+ $scope = if ( $pester .InTest ) { $null } else { $pester .CurrentTestGroup }
1107+ $Mock.CallHistory += @ {CommandName = " $ModuleName ||$CommandName " ; BoundParams = $BoundParameters ; Args = $ArgumentList ; Scope = $scope }
11081108
11091109 $scriptBlock = {
11101110 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
@@ -293,6 +294,21 @@ function New-PesterState
293294 return $script :TestGroupStack.Peek ().AfterAll
294295 }
295296
297+ function EnterTest
298+ {
299+ if ($script :InTest )
300+ {
301+ throw ' You are already in a test case.'
302+ }
303+
304+ $script :InTest = $true
305+ }
306+
307+ function LeaveTest
308+ {
309+ $script :InTest = $false
310+ }
311+
296312 $ExportedVariables = " TagFilter" ,
297313 " ExcludeTagFilter" ,
298314 " TestNameFilter" ,
@@ -311,7 +327,8 @@ function New-PesterState
311327 " IncludeVSCodeMarker" ,
312328 " TestActions" ,
313329 " TestGroupStack" ,
314- " TestSuiteName"
330+ " TestSuiteName" ,
331+ " InTest"
315332
316333 $ExportedFunctions = " EnterTestGroup" ,
317334 " LeaveTestGroup" ,
@@ -320,7 +337,9 @@ function New-PesterState
320337 " GetTestCaseSetupBlocks" ,
321338 " GetTestCaseTeardownBlocks" ,
322339 " GetCurrentTestGroupSetupBlocks" ,
323- " GetCurrentTestGroupTeardownBlocks"
340+ " GetCurrentTestGroupTeardownBlocks" ,
341+ " EnterTest" ,
342+ " LeaveTest"
324343
325344 & $SafeCommands [' Export-ModuleMember' ] - Variable $ExportedVariables - function $ExportedFunctions
326345 } |
You can’t perform that action at this time.
0 commit comments