Skip to content

Commit 47f90f2

Browse files
committed
Refactor tests to replace Invoke-CimMethod with Invoke-RsCimMethod in Disable-SqlDscRsSecureConnection and Enable-SqlDscRsSecureConnection test suites
1 parent 531a5fb commit 47f90f2

File tree

2 files changed

+20
-174
lines changed

2 files changed

+20
-174
lines changed

tests/Unit/Public/Disable-SqlDscRsSecureConnection.Tests.ps1

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,6 @@ AfterAll {
4444
}
4545

4646
Describe 'Disable-SqlDscRsSecureConnection' {
47-
BeforeAll {
48-
InModuleScope -ScriptBlock {
49-
function script:Invoke-CimMethod
50-
{
51-
param
52-
(
53-
[Parameter(ValueFromPipeline = $true)]
54-
[System.Object]
55-
$InputObject,
56-
57-
[System.String]
58-
$MethodName,
59-
60-
[System.Collections.Hashtable]
61-
$Arguments
62-
)
63-
64-
$PSCmdlet.ThrowTerminatingError(
65-
[System.Management.Automation.ErrorRecord]::new(
66-
'StubNotImplemented',
67-
'StubCalledError',
68-
[System.Management.Automation.ErrorCategory]::InvalidOperation,
69-
$MyInvocation.MyCommand
70-
)
71-
)
72-
}
73-
}
74-
}
75-
76-
AfterAll {
77-
InModuleScope -ScriptBlock {
78-
Remove-Item -Path 'function:script:Invoke-CimMethod' -Force
79-
}
80-
}
81-
8247
Context 'When validating parameter sets' {
8348
It 'Should have the correct parameters in parameter set <ExpectedParameterSetName>' -ForEach @(
8449
@{
@@ -105,7 +70,7 @@ Describe 'Disable-SqlDscRsSecureConnection' {
10570
SecureConnectionLevel = 2
10671
}
10772

108-
Mock -CommandName Invoke-CimMethod -MockWith {
73+
Mock -CommandName Invoke-RsCimMethod -MockWith {
10974
return [PSCustomObject] @{
11075
HRESULT = 0
11176
}
@@ -115,7 +80,7 @@ Describe 'Disable-SqlDscRsSecureConnection' {
11580
It 'Should disable secure connection without errors' {
11681
{ $mockCimInstance | Disable-SqlDscRsSecureConnection -Confirm:$false } | Should -Not -Throw
11782

118-
Should -Invoke -CommandName Invoke-CimMethod -ParameterFilter {
83+
Should -Invoke -CommandName Invoke-RsCimMethod -ParameterFilter {
11984
$MethodName -eq 'SetSecureConnectionLevel' -and
12085
$Arguments.Level -eq 0
12186
} -Exactly -Times 1
@@ -135,7 +100,7 @@ Describe 'Disable-SqlDscRsSecureConnection' {
135100
SecureConnectionLevel = 2
136101
}
137102

138-
Mock -CommandName Invoke-CimMethod -MockWith {
103+
Mock -CommandName Invoke-RsCimMethod -MockWith {
139104
return [PSCustomObject] @{
140105
HRESULT = 0
141106
}
@@ -157,7 +122,7 @@ Describe 'Disable-SqlDscRsSecureConnection' {
157122
SecureConnectionLevel = 2
158123
}
159124

160-
Mock -CommandName Invoke-CimMethod -MockWith {
125+
Mock -CommandName Invoke-RsCimMethod -MockWith {
161126
return [PSCustomObject] @{
162127
HRESULT = 0
163128
}
@@ -167,49 +132,7 @@ Describe 'Disable-SqlDscRsSecureConnection' {
167132
It 'Should disable secure connection without confirmation' {
168133
{ $mockCimInstance | Disable-SqlDscRsSecureConnection -Force } | Should -Not -Throw
169134

170-
Should -Invoke -CommandName Invoke-CimMethod -Exactly -Times 1
171-
}
172-
}
173-
174-
Context 'When CIM method fails with ExtendedErrors' {
175-
BeforeAll {
176-
$mockCimInstance = [PSCustomObject] @{
177-
InstanceName = 'SSRS'
178-
SecureConnectionLevel = 2
179-
}
180-
181-
Mock -CommandName Invoke-CimMethod -MockWith {
182-
$result = [PSCustomObject] @{
183-
HRESULT = -2147024891
184-
ExtendedErrors = @('Access denied', 'Permission error')
185-
}
186-
$result | Add-Member -MemberType ScriptMethod -Name 'GetType' -Value { return [PSCustomObject] @{ Name = 'CimMethodResult' } } -Force
187-
return $result
188-
}
189-
}
190-
191-
It 'Should throw a terminating error' {
192-
{ $mockCimInstance | Disable-SqlDscRsSecureConnection -Confirm:$false } | Should -Throw -ErrorId 'DSRSSC0001,Disable-SqlDscRsSecureConnection'
193-
}
194-
}
195-
196-
Context 'When CIM method fails with Error property' {
197-
BeforeAll {
198-
$mockCimInstance = [PSCustomObject] @{
199-
InstanceName = 'SSRS'
200-
SecureConnectionLevel = 2
201-
}
202-
203-
Mock -CommandName Invoke-CimMethod -MockWith {
204-
return [PSCustomObject] @{
205-
HRESULT = -2147024891
206-
Error = 'Access denied'
207-
}
208-
}
209-
}
210-
211-
It 'Should throw a terminating error' {
212-
{ $mockCimInstance | Disable-SqlDscRsSecureConnection -Confirm:$false } | Should -Throw -ErrorId 'DSRSSC0001,Disable-SqlDscRsSecureConnection'
135+
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 1
213136
}
214137
}
215138

@@ -220,13 +143,13 @@ Describe 'Disable-SqlDscRsSecureConnection' {
220143
SecureConnectionLevel = 2
221144
}
222145

223-
Mock -CommandName Invoke-CimMethod
146+
Mock -CommandName Invoke-RsCimMethod
224147
}
225148

226-
It 'Should not call Invoke-CimMethod' {
149+
It 'Should not call Invoke-RsCimMethod' {
227150
$mockCimInstance | Disable-SqlDscRsSecureConnection -WhatIf
228151

229-
Should -Invoke -CommandName Invoke-CimMethod -Exactly -Times 0
152+
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 0
230153
}
231154
}
232155

@@ -237,7 +160,7 @@ Describe 'Disable-SqlDscRsSecureConnection' {
237160
SecureConnectionLevel = 2
238161
}
239162

240-
Mock -CommandName Invoke-CimMethod -MockWith {
163+
Mock -CommandName Invoke-RsCimMethod -MockWith {
241164
return [PSCustomObject] @{
242165
HRESULT = 0
243166
}
@@ -247,7 +170,7 @@ Describe 'Disable-SqlDscRsSecureConnection' {
247170
It 'Should disable secure connection' {
248171
{ Disable-SqlDscRsSecureConnection -Configuration $mockCimInstance -Confirm:$false } | Should -Not -Throw
249172

250-
Should -Invoke -CommandName Invoke-CimMethod -Exactly -Times 1
173+
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 1
251174
}
252175
}
253176
}

tests/Unit/Public/Enable-SqlDscRsSecureConnection.Tests.ps1

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,6 @@ AfterAll {
4444
}
4545

4646
Describe 'Enable-SqlDscRsSecureConnection' {
47-
BeforeAll {
48-
InModuleScope -ScriptBlock {
49-
function script:Invoke-CimMethod
50-
{
51-
param
52-
(
53-
[Parameter(ValueFromPipeline = $true)]
54-
[System.Object]
55-
$InputObject,
56-
57-
[System.String]
58-
$MethodName,
59-
60-
[System.Collections.Hashtable]
61-
$Arguments
62-
)
63-
64-
$PSCmdlet.ThrowTerminatingError(
65-
[System.Management.Automation.ErrorRecord]::new(
66-
'StubNotImplemented',
67-
'StubCalledError',
68-
[System.Management.Automation.ErrorCategory]::InvalidOperation,
69-
$MyInvocation.MyCommand
70-
)
71-
)
72-
}
73-
}
74-
}
75-
76-
AfterAll {
77-
InModuleScope -ScriptBlock {
78-
Remove-Item -Path 'function:script:Invoke-CimMethod' -Force
79-
}
80-
}
81-
8247
Context 'When validating parameter sets' {
8348
It 'Should have the correct parameters in parameter set <ExpectedParameterSetName>' -ForEach @(
8449
@{
@@ -105,7 +70,7 @@ Describe 'Enable-SqlDscRsSecureConnection' {
10570
SecureConnectionLevel = 0
10671
}
10772

108-
Mock -CommandName Invoke-CimMethod -MockWith {
73+
Mock -CommandName Invoke-RsCimMethod -MockWith {
10974
return [PSCustomObject] @{
11075
HRESULT = 0
11176
}
@@ -115,7 +80,7 @@ Describe 'Enable-SqlDscRsSecureConnection' {
11580
It 'Should enable secure connection without errors' {
11681
{ $mockCimInstance | Enable-SqlDscRsSecureConnection -Confirm:$false } | Should -Not -Throw
11782

118-
Should -Invoke -CommandName Invoke-CimMethod -ParameterFilter {
83+
Should -Invoke -CommandName Invoke-RsCimMethod -ParameterFilter {
11984
$MethodName -eq 'SetSecureConnectionLevel' -and
12085
$Arguments.Level -eq 1
12186
} -Exactly -Times 1
@@ -135,7 +100,7 @@ Describe 'Enable-SqlDscRsSecureConnection' {
135100
SecureConnectionLevel = 0
136101
}
137102

138-
Mock -CommandName Invoke-CimMethod -MockWith {
103+
Mock -CommandName Invoke-RsCimMethod -MockWith {
139104
return [PSCustomObject] @{
140105
HRESULT = 0
141106
}
@@ -157,7 +122,7 @@ Describe 'Enable-SqlDscRsSecureConnection' {
157122
SecureConnectionLevel = 0
158123
}
159124

160-
Mock -CommandName Invoke-CimMethod -MockWith {
125+
Mock -CommandName Invoke-RsCimMethod -MockWith {
161126
return [PSCustomObject] @{
162127
HRESULT = 0
163128
}
@@ -167,49 +132,7 @@ Describe 'Enable-SqlDscRsSecureConnection' {
167132
It 'Should enable secure connection without confirmation' {
168133
{ $mockCimInstance | Enable-SqlDscRsSecureConnection -Force } | Should -Not -Throw
169134

170-
Should -Invoke -CommandName Invoke-CimMethod -Exactly -Times 1
171-
}
172-
}
173-
174-
Context 'When CIM method fails with ExtendedErrors' {
175-
BeforeAll {
176-
$mockCimInstance = [PSCustomObject] @{
177-
InstanceName = 'SSRS'
178-
SecureConnectionLevel = 0
179-
}
180-
181-
Mock -CommandName Invoke-CimMethod -MockWith {
182-
$result = [PSCustomObject] @{
183-
HRESULT = -2147024891
184-
ExtendedErrors = @('Access denied', 'Permission error')
185-
}
186-
$result | Add-Member -MemberType ScriptMethod -Name 'GetType' -Value { return [PSCustomObject] @{ Name = 'CimMethodResult' } } -Force
187-
return $result
188-
}
189-
}
190-
191-
It 'Should throw a terminating error' {
192-
{ $mockCimInstance | Enable-SqlDscRsSecureConnection -Confirm:$false } | Should -Throw -ErrorId 'ESRSSC0001,Enable-SqlDscRsSecureConnection'
193-
}
194-
}
195-
196-
Context 'When CIM method fails with Error property' {
197-
BeforeAll {
198-
$mockCimInstance = [PSCustomObject] @{
199-
InstanceName = 'SSRS'
200-
SecureConnectionLevel = 0
201-
}
202-
203-
Mock -CommandName Invoke-CimMethod -MockWith {
204-
return [PSCustomObject] @{
205-
HRESULT = -2147024891
206-
Error = 'Access denied'
207-
}
208-
}
209-
}
210-
211-
It 'Should throw a terminating error' {
212-
{ $mockCimInstance | Enable-SqlDscRsSecureConnection -Confirm:$false } | Should -Throw -ErrorId 'ESRSSC0001,Enable-SqlDscRsSecureConnection'
135+
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 1
213136
}
214137
}
215138

@@ -220,13 +143,13 @@ Describe 'Enable-SqlDscRsSecureConnection' {
220143
SecureConnectionLevel = 0
221144
}
222145

223-
Mock -CommandName Invoke-CimMethod
146+
Mock -CommandName Invoke-RsCimMethod
224147
}
225148

226-
It 'Should not call Invoke-CimMethod' {
149+
It 'Should not call Invoke-RsCimMethod' {
227150
$mockCimInstance | Enable-SqlDscRsSecureConnection -WhatIf
228151

229-
Should -Invoke -CommandName Invoke-CimMethod -Exactly -Times 0
152+
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 0
230153
}
231154
}
232155

@@ -237,7 +160,7 @@ Describe 'Enable-SqlDscRsSecureConnection' {
237160
SecureConnectionLevel = 0
238161
}
239162

240-
Mock -CommandName Invoke-CimMethod -MockWith {
163+
Mock -CommandName Invoke-RsCimMethod -MockWith {
241164
return [PSCustomObject] @{
242165
HRESULT = 0
243166
}
@@ -247,7 +170,7 @@ Describe 'Enable-SqlDscRsSecureConnection' {
247170
It 'Should enable secure connection' {
248171
{ Enable-SqlDscRsSecureConnection -Configuration $mockCimInstance -Confirm:$false } | Should -Not -Throw
249172

250-
Should -Invoke -CommandName Invoke-CimMethod -Exactly -Times 1
173+
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 1
251174
}
252175
}
253176
}

0 commit comments

Comments
 (0)