Test the guarantees of a change tool
Write Pester checks that detect unintended writes, lost settings, and misleading repeat-run results.
Test effects, not reassuring messages
The supplied ManagedSettings.ps1 contains Set-ManagedRetention. It should preview without writing, apply the requested setting while preserving LocalNote, and report no new change on a second identical call. Your job is to test those guarantees against files and result objects.
Load the helper in BeforeAll. Place BeforeEach inside Describe to copy base-settings.json to a scratch file such as test-settings.json. Each test then starts from the same known state instead of depending on what the previous test left behind.
Describe 'Managed configuration' {
BeforeEach {
Copy-Item '/home/user/base-settings.json' '/home/user/test-settings.json' -Force
}
# Add the three It blocks here.
}
Check three distinct behaviours
The WhatIf test should compare file hashes before and after the call and assert Changed false. The normal-update test should re-import JSON, check LogRetentionDays 30 and LocalNote preserve, and assert Changed true. The repeat test should call the function twice, assert the second Changed is false, and verify that the second call leaves bytes unchanged.
Save exactly three unskipped It tests in ManagedSettings.Tests.ps1, then invoke Pester with -PassThru -Output None. Pester is already bundled in this lab.
The course checker temporarily substitutes three broken implementations: one writes during WhatIf, one falsely claims repeated changes, and one discards unrelated settings. Your suite must reject each. These mutation checks make sure your assertions can detect the mistakes they claim to guard against; constant assertions that merely pass will not complete the exercise.
Go deeper
. '/home/user/ManagedSettings.ps1'
Set-ManagedRetention '/home/user/base-settings.json' -Days 30 -WhatIf
Put it into practice.
Create ManagedSettings.Tests.ps1 with exactly three unskipped Pester tests for the supplied Set-ManagedRetention helper: WhatIf preserves bytes and returns Changed false; a normal call sets LogRetentionDays to 30 while preserving LocalNote and returns Changed true; a second identical call preserves bytes and returns Changed false. Load the helper in BeforeAll and put BeforeEach inside Describe to create a fresh scratch copy per test. Run with -PassThru -Output None and emit the result. Do not edit the helper or baseline.
See an explained solution
@'
BeforeAll { . '/home/user/ManagedSettings.ps1' }
Describe 'Managed configuration' {
BeforeEach { Copy-Item '/home/user/base-settings.json' '/home/user/test-settings.json' -Force }
It 'does not write during WhatIf' {
$before = (Get-FileHash '/home/user/test-settings.json').Hash
$result = Set-ManagedRetention '/home/user/test-settings.json' -Days 30 -WhatIf
$result.Changed | Should -BeFalse
(Get-FileHash '/home/user/test-settings.json').Hash | Should -Be $before
}
It 'updates the setting and preserves unrelated data' {
$result = Set-ManagedRetention '/home/user/test-settings.json' -Days 30 -Confirm:$false
$result.Changed | Should -BeTrue
$settings = Get-Content '/home/user/test-settings.json' -Raw | ConvertFrom-Json
$settings.LogRetentionDays | Should -Be 30
$settings.LocalNote | Should -Be 'preserve'
}
It 'does no new work on the second identical call' {
Set-ManagedRetention '/home/user/test-settings.json' -Days 30 -Confirm:$false | Out-Null
$before = (Get-FileHash '/home/user/test-settings.json').Hash
$result = Set-ManagedRetention '/home/user/test-settings.json' -Days 30 -Confirm:$false
$result.Changed | Should -BeFalse
(Get-FileHash '/home/user/test-settings.json').Hash | Should -Be $before
}
}
'@ | Set-Content '/home/user/ManagedSettings.Tests.ps1'
Invoke-Pester '/home/user/ManagedSettings.Tests.ps1' -PassThru -Output None
Each test starts from a clean copy and checks an externally visible guarantee. The broken helper variants prove that the tests notice writes, false change reports, and lost settings instead of simply reporting success.