Skip to content

Commit f3a9647

Browse files
Copilotjohlju
andcommitted
Fix unit tests for Pester v6 compatibility and improve copilot instructions
Co-authored-by: johlju <[email protected]>
1 parent 7d6f7ba commit f3a9647

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

.github/copilot-instructions.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ should be written to cover all possible scenarios and code paths, ensuring
214214
that both edge cases and common use cases are tested.
215215

216216
All public commands should always have a test to validate parameter sets
217-
using this template:
217+
using this template. For commands with a single parameter set:
218218

219219
```powershell
220220
It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @(
@@ -243,6 +243,53 @@ It 'Should have the correct parameters in parameter set <MockParameterSetName>'
243243
}
244244
```
245245

246+
For commands with multiple parameter sets, use this pattern:
247+
248+
```powershell
249+
It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @(
250+
@{
251+
MockParameterSetName = 'ParameterSet1'
252+
MockExpectedParameters = '-ServerObject <Server> -Name <string> -Parameter1 <string> [<CommonParameters>]'
253+
}
254+
@{
255+
MockParameterSetName = 'ParameterSet2'
256+
MockExpectedParameters = '-ServerObject <Server> -Name <string> -Parameter2 <uint> [<CommonParameters>]'
257+
}
258+
) {
259+
$result = (Get-Command -Name 'CommandName').ParameterSets |
260+
Where-Object -FilterScript {
261+
$_.Name -eq $mockParameterSetName
262+
} |
263+
Select-Object -Property @(
264+
@{
265+
Name = 'ParameterSetName'
266+
Expression = { $_.Name }
267+
},
268+
@{
269+
Name = 'ParameterListAsString'
270+
Expression = { $_.ToString() }
271+
}
272+
)
273+
274+
$result.ParameterSetName | Should -Be $MockParameterSetName
275+
$result.ParameterListAsString | Should -Be $MockExpectedParameters
276+
}
277+
```
278+
279+
All public commands should also include tests to validate parameter properties:
280+
281+
```powershell
282+
It 'Should have ParameterName as a mandatory parameter' {
283+
$parameterInfo = (Get-Command -Name 'CommandName').Parameters['ParameterName']
284+
$parameterInfo.Attributes.Mandatory | Should -Contain $true
285+
}
286+
287+
It 'Should accept ParameterName from pipeline' {
288+
$parameterInfo = (Get-Command -Name 'CommandName').Parameters['ParameterName']
289+
$parameterInfo.Attributes.ValueFromPipeline | Should -Contain $true
290+
}
291+
```
292+
246293
The `BeforeAll` block should be used to set up any necessary test data or mocking
247294

248295
Use localized strings in the tests only when necessary. You can assign the

tests/Unit/Public/Assert-SqlDscLogin.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ Describe 'Assert-SqlDscLogin' -Tag 'Public' {
6464

6565
It 'Should call Test-SqlDscIsLogin with correct parameters' {
6666
Assert-SqlDscLogin -ServerObject $mockServerObject -Name 'TestLogin'
67-
67+
6868
Should -Invoke -CommandName 'Test-SqlDscIsLogin' -ParameterFilter {
6969
$ServerObject.InstanceName -eq 'TestInstance' -and
7070
$Name -eq 'TestLogin'
7171
} -Exactly -Times 1
7272
}
7373

7474
It 'Should accept ServerObject from pipeline' {
75-
{ $mockServerObject | Assert-SqlDscLogin -Name 'TestLogin' } | Should -Not -Throw
75+
$mockServerObject | Assert-SqlDscLogin -Name 'TestLogin'
7676
}
7777
}
7878

@@ -110,7 +110,7 @@ Describe 'Assert-SqlDscLogin' -Tag 'Public' {
110110

111111
It 'Should call Test-SqlDscIsLogin with correct parameters' {
112112
{ Assert-SqlDscLogin -ServerObject $mockServerObject -Name 'NonExistentLogin' } | Should -Throw
113-
113+
114114
Should -Invoke -CommandName 'Test-SqlDscIsLogin' -ParameterFilter {
115115
$ServerObject.InstanceName -eq 'TestInstance' -and
116116
$Name -eq 'NonExistentLogin'

0 commit comments

Comments
 (0)