From 581229f5c886a2faf46d4e67d9bcee768ac82c22 Mon Sep 17 00:00:00 2001 From: "Vic Perdana (MSFT)" <7114832+vicperdana@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:51:07 +1000 Subject: [PATCH] Added `InModuleScope` --- tests/PSDocs.Tests/PSDocs.Common.Tests.ps1 | 39 +++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/tests/PSDocs.Tests/PSDocs.Common.Tests.ps1 b/tests/PSDocs.Tests/PSDocs.Common.Tests.ps1 index 66b0f7c..b5d727d 100644 --- a/tests/PSDocs.Tests/PSDocs.Common.Tests.ps1 +++ b/tests/PSDocs.Tests/PSDocs.Common.Tests.ps1 @@ -249,24 +249,31 @@ Describe 'Get-PSDocument' -Tag 'Cmdlet', 'Common', 'Get-PSDocument' { $Null = Remove-Module -Name TestModule; } - It 'Loads module with preference' { - try { - # Test negative case - $Global:PSModuleAutoLoadingPreference = [System.Management.Automation.PSModuleAutoLoadingPreference]::None; - $Null = Get-PSDocument -Module 'TestModule'; - { Get-Module -Name 'PSDocs' } | Should -Throw; + InModuleScope PSDocs { - # Test positive case - $Global:PSModuleAutoLoadingPreference = [System.Management.Automation.PSModuleAutoLoadingPreference]::All; - $Null = Get-PSDocument -Module 'TestModule'; - { Get-Module -Name 'PSDocs' } | Should -Not -Throw; - } - finally { - if ($Null -eq $currentLoadingPreference) { - Remove-Variable -Name PSModuleAutoLoadingPreference -Force -ErrorAction SilentlyContinue; + It 'Loads module with preference' { + try { + # Test negative case + $Global:PSModuleAutoLoadingPreference = [System.Management.Automation.PSModuleAutoLoadingPreference]::None + Write-Host "Calling Get-PSDocument with preference set to None" + $Null = Get-PSDocument -Module 'TestModule' + Assert-MockCalled -CommandName 'LoadModule' -ModuleName 'PSDocs' -Times 0 -Scope 'It' + + # Test positive case + $Global:PSModuleAutoLoadingPreference = [System.Management.Automation.PSModuleAutoLoadingPreference]::All + Write-Host "Calling Get-PSDocument with preference set to All" + $Null = Get-PSDocument -Module 'TestModule' + Assert-MockCalled -CommandName 'LoadModule' -ModuleName 'PSDocs' -Times 1 -Scope 'It' -Exactly + + Assert-VerifiableMocks } - else { - $Global:PSModuleAutoLoadingPreference = $currentLoadingPreference; + finally { + if ($Null -eq $currentLoadingPreference) { + Remove-Variable -Name PSModuleAutoLoadingPreference -Force -ErrorAction SilentlyContinue + } + else { + $Global:PSModuleAutoLoadingPreference = $currentLoadingPreference + } } } }