Created
April 23, 2021 14:36
-
-
Save fflaten/d7529c5ea9cf0dbd668a67155af64299 to your computer and use it in GitHub Desktop.
pester mock in module demo #1915
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
Describe 'Module-scoped mocks' { | |
BeforeAll { | |
$module = "demoModule" | |
$m = New-Module -Name $module -ScriptBlock { | |
function Invoke-SomeAction { | |
[CmdletBinding()] | |
param ( | |
) | |
$AJob = Start-Job -InitializationScript {"Some Init stuff"} -ScriptBlock {"The scriptblock"} | |
} | |
Export-ModuleMember Invoke-SomeAction | |
} | |
$m | Import-Module | |
} | |
Context "Using ModuleName" { | |
BeforeAll { | |
Mock Start-Job -MockWith { | |
Write-Warning "Mocked scriptblock using ModuleName" | |
} -ModuleName $module | |
} | |
It "Should invoke without ModuleName - should not match module-mock" { | |
Invoke-SomeAction | |
Should -Invoke -CommandName Start-Job -Times 0 -Scope It | |
} | |
It "Should invoke with ModuleName - should match module-mock" { | |
Invoke-SomeAction | |
Should -Invoke -CommandName Start-Job -ModuleName $module -Times 1 -Scope It | |
} | |
} | |
Context "Using InModuleScope" { | |
BeforeAll { | |
InModuleScope -ModuleName $module -Scriptblock { | |
Mock Start-Job -MockWith { | |
Write-Warning "Mocked scriptblock using InModuleScope" | |
} | |
} | |
} | |
It "Should invoke without ModuleName - should not match module-mock" { | |
Invoke-SomeAction | |
Should -Invoke -CommandName Start-Job -Times 0 -Scope It | |
} | |
It "Should invoke with ModuleName - should match module-mock" { | |
Invoke-SomeAction | |
Should -Invoke -CommandName Start-Job -ModuleName $module -Times 1 -Scope It | |
} | |
} | |
AfterAll { | |
$m | Remove-Module | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment