diff --git a/CHANGELOG.md b/CHANGELOG.md index f8016254..1b8d9478 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md) ### Changed +- `ActiveDirectoryDsc` + - Updated unit tests for Pester 6 compatibility - forward unmatched `Test-Path` + mock calls to the real cmdlet, suppress unmatched `Write-Verbose` calls, and + replace the removed `Assert-VerifiableMock` with `Should -InvokeVerifiable`. - README - Updated PSGallery Pre-Release badge. - Examples diff --git a/RequiredModules.psd1 b/RequiredModules.psd1 index 7c9328ee..10e4d97c 100644 --- a/RequiredModules.psd1 +++ b/RequiredModules.psd1 @@ -9,7 +9,9 @@ InvokeBuild = 'latest' PSScriptAnalyzer = 'latest' - Pester = 'latest' + Pester = @{ + Version = '6.0.0' + } Plaster = 'latest' ModuleBuilder = 'latest' ChangelogManagement = 'latest' diff --git a/tests/Unit/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.Tests.ps1 b/tests/Unit/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.Tests.ps1 index 145f850e..84082d8d 100644 --- a/tests/Unit/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.Tests.ps1 +++ b/tests/Unit/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.Tests.ps1 @@ -756,7 +756,7 @@ Describe 'ActiveDirectoryDsc.Common\Restore-ADCommonObject' { } It 'Should call Get-ADObject as well as Restore-ADObject' { - Assert-VerifiableMock + Should -InvokeVerifiable } It 'Should throw an InvalidOperationException when object parent does not exist' { @@ -1558,7 +1558,7 @@ Describe 'ActiveDirectoryDsc.Common\Get-ADDirectoryContext' { } } - Assert-VerifiableMock + Should -InvokeVerifiable } } @@ -1625,7 +1625,7 @@ Describe 'ActiveDirectoryDsc.Common\Find-DomainController' -Tag 'FindDomainContr } } - Assert-VerifiableMock + Should -InvokeVerifiable } Context 'When no domain controller is found' { @@ -1647,6 +1647,9 @@ Describe 'ActiveDirectoryDsc.Common\Find-DomainController' -Tag 'FindDomainContr ) } + # Pester v6 throws instead of calling the real command when no -ParameterFilter + # matches. Suppress unmatched Write-Verbose calls (v5 let them through). + Mock -CommandName Write-Verbose -MockWith { } Mock -CommandName Write-Verbose -ParameterFilter { $Message -like 'Searching for a domain controller*' } -MockWith { @@ -1658,10 +1661,12 @@ Describe 'ActiveDirectoryDsc.Common\Find-DomainController' -Tag 'FindDomainContr { Find-DomainController -DomainName $mockDomainName } | Should -Not -Throw Should -Invoke -CommandName Find-DomainControllerFindOneWrapper -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Write-Verbose -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Write-Verbose -ParameterFilter { + $Message -like 'Searching for a domain controller*' + } -Exactly -Times 1 -Scope It } - Assert-VerifiableMock + Should -InvokeVerifiable } Context 'When the lookup for a domain controller fails' { @@ -1686,7 +1691,7 @@ Describe 'ActiveDirectoryDsc.Common\Find-DomainController' -Tag 'FindDomainContr Should -Invoke -CommandName Find-DomainControllerFindOneWrapper -Exactly -Times 1 -Scope It } - Assert-VerifiableMock + Should -InvokeVerifiable } Context 'When the Find-DomainController throws an authentication exception' { diff --git a/tests/Unit/MSFT_ADDomain.Tests.ps1 b/tests/Unit/MSFT_ADDomain.Tests.ps1 index 7cc12455..eab2b89d 100644 --- a/tests/Unit/MSFT_ADDomain.Tests.ps1 +++ b/tests/Unit/MSFT_ADDomain.Tests.ps1 @@ -62,6 +62,9 @@ AfterAll { Describe 'MSFT_ADDomain\Get-TargetResource' -Tag 'Get' { BeforeAll { Mock -CommandName Assert-Module + # Pester v6 throws instead of calling the real command when no -ParameterFilter + # matches. Forward unmatched Test-Path calls to the real cmdlet to keep v5 behaviour. + Mock -CommandName Test-Path -MockWith { & (Get-Command -Name 'Test-Path' -CommandType Cmdlet) @PesterBoundParameters } Mock -CommandName Test-Path -ParameterFilter { $Path -eq 'C:\Windows\SysVol\contoso.com' } -MockWith { $true } @@ -675,6 +678,9 @@ Describe 'MSFT_ADDomain\Set-TargetResource' -Tag 'Set' { Context 'When the domain controller is pending reboot and SuppressReboot is $false' { BeforeAll { # Make the resource think a reboot is pending after installation. + # Pester v6 throws instead of calling the real command when no -ParameterFilter + # matches. Forward unmatched Test-Path calls to the real cmdlet to keep v5 behaviour. + Mock -CommandName Test-Path -MockWith { & (Get-Command -Name 'Test-Path' -CommandType Cmdlet) @PesterBoundParameters } Mock -CommandName Test-Path -ParameterFilter { $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters\LocatorDCPromoPreRebootHint' } -MockWith { $true } @@ -1078,6 +1084,9 @@ Describe 'MSFT_ADDomain\Set-TargetResource' -Tag 'Set' { Context 'When the domain controller is pending reboot and SuppressReboot is $false' { BeforeAll { + # Pester v6 throws instead of calling the real command when no -ParameterFilter + # matches. Forward unmatched Test-Path calls to the real cmdlet to keep v5 behaviour. + Mock -CommandName Test-Path -MockWith { & (Get-Command -Name 'Test-Path' -CommandType Cmdlet) @PesterBoundParameters } Mock -CommandName Test-Path -ParameterFilter { $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters\LocatorDCPromoPreRebootHint' } -MockWith { $true } diff --git a/tests/Unit/MSFT_WaitForADDomain.Tests.ps1 b/tests/Unit/MSFT_WaitForADDomain.Tests.ps1 index 035fc4fb..2f8feecb 100644 --- a/tests/Unit/MSFT_WaitForADDomain.Tests.ps1 +++ b/tests/Unit/MSFT_WaitForADDomain.Tests.ps1 @@ -157,6 +157,9 @@ Describe 'MSFT_WaitForADDomain\Get-TargetResource' -Tag 'Get' { Context 'When using BuiltInCredential parameter' { BeforeAll { + # Pester v6 throws instead of calling the real command when no -ParameterFilter + # matches. Suppress unmatched Write-Verbose calls (v5 let them through). + Mock -CommandName Write-Verbose -MockWith { } Mock -CommandName Write-Verbose -ParameterFilter { $Message -like 'Impersonating the credentials ''BuiltInCredential''*' } -MockWith { @@ -185,7 +188,9 @@ Describe 'MSFT_WaitForADDomain\Get-TargetResource' -Tag 'Get' { $result.Credential | Should -BeNullOrEmpty } - Should -Invoke -CommandName Write-Verbose -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Write-Verbose -ParameterFilter { + $Message -like 'Impersonating the credentials ''BuiltInCredential''*' + } -Exactly -Times 1 -Scope It } } }