Skip to content

Commit 7285034

Browse files
committed
Enhance error handling in Get-SqlDscServerProtocolTcpIp and add unit tests for Protocol and Port properties in SqlResourceBase
1 parent cdbcf50 commit 7285034

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

source/Public/Get-SqlDscServerProtocolTcpIp.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ function Get-SqlDscServerProtocolTcpIp
149149
$errorMessage = $script:localizedData.ServerProtocolTcpIp_InvalidProtocol -f $ServerProtocolObject.Name
150150

151151
$writeErrorParameters = @{
152-
Exception = [System.ArgumentException]::new($errorMessage)
153-
ErrorId = 'InvalidServerProtocol'
152+
Exception = New-ArgumentException -Message $errorMessage -ArgumentName 'ServerProtocolObject' -PassThru
153+
ErrorId = 'InvalidServerProtocol'
154154
ErrorCategory = 'InvalidArgument'
155-
TargetObject = $ServerProtocolObject
155+
TargetObject = $ServerProtocolObject
156156
}
157157

158158
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @writeErrorParameters))
@@ -185,10 +185,10 @@ function Get-SqlDscServerProtocolTcpIp
185185
$errorMessage = $script:localizedData.ServerProtocolTcpIp_IpAddressGroupNotFound -f $IpAddressGroup
186186

187187
$writeErrorParameters = @{
188-
Exception = [System.InvalidOperationException]::new($errorMessage)
189-
ErrorId = 'IpAddressGroupNotFound'
188+
Exception = New-InvalidOperationException -Message $errorMessage -PassThru
189+
ErrorId = 'IpAddressGroupNotFound'
190190
ErrorCategory = 'ObjectNotFound'
191-
TargetObject = $IpAddressGroup
191+
TargetObject = $IpAddressGroup
192192
}
193193

194194
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @writeErrorParameters))

tests/Unit/Classes/SqlResourceBase.Tests.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,46 @@ Describe 'SqlResourceBase\GetServerObject()' -Tag 'GetServerObject' {
121121
} -Exactly -Times 1 -Scope It
122122
}
123123
}
124+
125+
Context 'When property Protocol is used' {
126+
BeforeAll {
127+
$mockSqlResourceBaseInstance = InModuleScope -ScriptBlock {
128+
[SqlResourceBase]::new()
129+
}
130+
131+
$mockSqlResourceBaseInstance.Protocol = 'tcp'
132+
}
133+
134+
It 'Should call the correct mock' {
135+
$result = $mockSqlResourceBaseInstance.GetServerObject()
136+
137+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Server'
138+
139+
Should -Invoke -CommandName Connect-SqlDscDatabaseEngine -ParameterFilter {
140+
$PesterBoundParameters.Keys -contains 'Protocol'
141+
} -Exactly -Times 1 -Scope It
142+
}
143+
}
144+
145+
Context 'When property Port is used' {
146+
BeforeAll {
147+
$mockSqlResourceBaseInstance = InModuleScope -ScriptBlock {
148+
[SqlResourceBase]::new()
149+
}
150+
151+
$mockSqlResourceBaseInstance.Port = 1433
152+
}
153+
154+
It 'Should call the correct mock' {
155+
$result = $mockSqlResourceBaseInstance.GetServerObject()
156+
157+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Server'
158+
159+
Should -Invoke -CommandName Connect-SqlDscDatabaseEngine -ParameterFilter {
160+
$PesterBoundParameters.Keys -contains 'Port'
161+
} -Exactly -Times 1 -Scope It
162+
}
163+
}
124164
}
125165

126166
Context 'When a server object already exist' {

0 commit comments

Comments
 (0)