Skip to content

Commit b2cfeb9

Browse files
authored
Merge pull request #747 from dlwyatt/MockFix3
Fixes #746 - Incorrect Mock call history scopes
2 parents e4e6075 + 7893e71 commit b2cfeb9

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

Functions/Describe.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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

Functions/It.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

Functions/Mock.Tests.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
17231748
Describe "Restoring original commands when mock scopes exit" {
17241749
function a (){}
17251750
Context "first context" {

Functions/Mock.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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 (

Functions/PesterState.ps1

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff 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
} |

0 commit comments

Comments
 (0)