Skip to content

Commit 74305f6

Browse files
nohwndCopilot
andcommitted
Add nested re-mock pattern to repro #2829
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6d2c3b6 commit 74305f6

1 file changed

Lines changed: 38 additions & 5 deletions

File tree

.repro2829/repro.ps1

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Import-Module (Join-Path $modDir 'ReproMod.psd1') -Force
100100
$container = New-PesterContainer -ScriptBlock {
101101
Describe 'Repro 2829' {
102102
BeforeAll {
103+
# Describe-scope (BeforeAll) default mocks, exactly like dbatools' BeforeAll.
103104
Mock -CommandName Invoke-Program -ModuleName ReproMod -MockWith {
104105
[PSCustomObject]@{ Successful = $true; ExitCode = [uint32[]]0 }
105106
}
@@ -111,11 +112,43 @@ $container = New-PesterContainer -ScriptBlock {
111112
# a -ParameterFilter mock, like dbatools' Get-DbaCmObject mock
112113
Mock -CommandName Get-Content -ModuleName ReproMod -MockWith { 'x' } -ParameterFilter { $Path -eq 'never' }
113114
}
114-
It 'engages the -ModuleName mock reached via ForEach-Object' {
115-
$result = Install-Thing -ComputerName 'HOST'
116-
Should -Invoke -CommandName Invoke-Program -Exactly 1 -Scope It -ModuleName ReproMod
117-
Should -Invoke -CommandName Test-PendingReboot -Exactly 1 -Scope It -ModuleName ReproMod
118-
$result.Successful | Should -Be $true
115+
116+
Context 'Validate installs of each version' {
117+
It 'A - relies on the Describe/BeforeAll mock only' {
118+
$result = Install-Thing -ComputerName 'HOST'
119+
Should -Invoke -CommandName Invoke-Program -Exactly 1 -Scope It -ModuleName ReproMod
120+
$result.Successful | Should -Be $true
121+
}
122+
It 'B - re-mocks Invoke-Program inside the It (nested), like the restart/tools tests' {
123+
Mock -CommandName Invoke-Program -ModuleName ReproMod -MockWith {
124+
[PSCustomObject]@{ Successful = $true; ExitCode = [uint32[]]3010 }
125+
}
126+
$result = Install-Thing -ComputerName 'HOST'
127+
$result.ExitCode | Should -Be 3010
128+
}
129+
It 'C - relies on the Describe mock again AFTER a nested re-mock' {
130+
$result = Install-Thing -ComputerName 'HOST'
131+
$result.Successful | Should -Be $true
132+
$result.ExitCode | Should -Be 0
133+
}
134+
}
135+
136+
Context 'Negative tests' {
137+
It 'D - re-mocks Invoke-Program to failure then reverts, like "fails when update execution has failed"' {
138+
#override default mock
139+
Mock -CommandName Invoke-Program -ModuleName ReproMod -MockWith {
140+
[PSCustomObject]@{ Successful = $false; ExitCode = [uint32[]]12345 }
141+
}
142+
(Install-Thing -ComputerName 'HOST').Successful | Should -Be $false
143+
(Install-Thing -ComputerName 'HOST').ExitCode | Should -Be 12345
144+
#revert default mock
145+
Mock -CommandName Invoke-Program -ModuleName ReproMod -MockWith {
146+
[PSCustomObject]@{ Successful = $true; ExitCode = [uint32[]]0 }
147+
}
148+
}
149+
It 'E - relies on the Describe mock again AFTER the negative re-mock+revert' {
150+
(Install-Thing -ComputerName 'HOST').Successful | Should -Be $true
151+
}
119152
}
120153
}
121154
}

0 commit comments

Comments
 (0)