@@ -145,6 +145,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
145145 $instance = [SqlAgentAlert ] @ {
146146 Name = ' TestAlert'
147147 InstanceName = ' MSSQLSERVER'
148+ Severity = 17
148149 } |
149150 Add-Member - Force - MemberType ' ScriptMethod' - Name ' GetServerObject' - Value {
150151 return $script :mockServerObject
@@ -168,6 +169,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
168169 $instance = [SqlAgentAlert ] @ {
169170 Name = ' TestAlert'
170171 InstanceName = ' MSSQLSERVER'
172+ Severity = 17
171173 } |
172174 Add-Member - Force - MemberType ' ScriptMethod' - Name ' GetServerObject' - Value {
173175 return $script :mockServerObject
@@ -340,14 +342,14 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
340342 }
341343
342344 Context ' When Ensure is Present' {
343- It ' Should not throw when neither Severity nor MessageId are specified' {
345+ It ' Should throw when neither Severity nor MessageId are specified' {
344346 InModuleScope - ScriptBlock {
345347 $properties = @ {
346348 Name = ' TestAlert'
347349 Ensure = ' Present'
348350 }
349351
350- { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should -Not - Throw
352+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw - ExpectedMessage ' *(DRC0052)* '
351353 }
352354 }
353355
@@ -406,7 +408,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
406408 Severity = 16
407409 }
408410
409- { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw - ExpectedMessage ' *Cannot specify Severity or MessageId when Ensure is set to '' Absent '' *'
411+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw - ExpectedMessage ' *(DRC0053) *'
410412 }
411413 }
412414
@@ -418,7 +420,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
418420 MessageId = 50001
419421 }
420422
421- { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw - ExpectedMessage ' *Cannot specify Severity or MessageId when Ensure is set to '' Absent '' *'
423+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw - ExpectedMessage ' *(DRC0053) *'
422424 }
423425 }
424426
@@ -431,7 +433,107 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
431433 MessageId = 50001
432434 }
433435
434- { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw - ExpectedMessage ' *Cannot specify Severity or MessageId when Ensure is set to '' Absent'' *'
436+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw - ExpectedMessage ' *(DRC0053)*'
437+ }
438+ }
439+ }
440+ }
441+
442+ Context ' When validating Assert-BoundParameter calls' {
443+ BeforeAll {
444+ InModuleScope - ScriptBlock {
445+ $script :mockSqlAgentAlertInstance = [SqlAgentAlert ]::new()
446+ }
447+ }
448+
449+ Context ' When Ensure is Present' {
450+ It ' Should call Assert-BoundParameter to validate at least one of Severity or MessageId is specified' {
451+ InModuleScope - ScriptBlock {
452+ Mock - CommandName ' Assert-BoundParameter' - MockWith { }
453+
454+ $properties = @ {
455+ Name = ' TestAlert'
456+ Ensure = ' Present'
457+ Severity = 16
458+ }
459+
460+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should -Not - Throw
461+
462+ Should - Invoke - CommandName ' Assert-BoundParameter' - ParameterFilter {
463+ $BoundParameterList -is [hashtable ] -and
464+ $AtLeastOneList -contains ' Severity' -and
465+ $AtLeastOneList -contains ' MessageId' -and
466+ $IfEqualParameterList.Ensure -eq ' Present'
467+ } - Exactly - Times 1 - Scope It
468+ }
469+ }
470+
471+ It ' Should call Assert-BoundParameter to validate Severity and MessageId are mutually exclusive' {
472+ InModuleScope - ScriptBlock {
473+ Mock - CommandName ' Assert-BoundParameter' - MockWith { }
474+
475+ $properties = @ {
476+ Name = ' TestAlert'
477+ Ensure = ' Present'
478+ Severity = 16
479+ }
480+
481+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should -Not - Throw
482+
483+ Should - Invoke - CommandName ' Assert-BoundParameter' - ParameterFilter {
484+ $BoundParameterList -is [hashtable ] -and
485+ $MutuallyExclusiveList1 -contains ' Severity' -and
486+ $MutuallyExclusiveList2 -contains ' MessageId' -and
487+ $IfEqualParameterList.Ensure -eq ' Present'
488+ } - Exactly - Times 1 - Scope It
489+ }
490+ }
491+ }
492+
493+ Context ' When Ensure is Absent' {
494+ It ' Should call Assert-BoundParameter to validate Severity and MessageId are not allowed' {
495+ InModuleScope - ScriptBlock {
496+ Mock - CommandName ' Assert-BoundParameter' - MockWith { }
497+
498+ $properties = @ {
499+ Name = ' TestAlert'
500+ Ensure = ' Absent'
501+ }
502+
503+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should -Not - Throw
504+
505+ Should - Invoke - CommandName ' Assert-BoundParameter' - ParameterFilter {
506+ $BoundParameterList -is [hashtable ] -and
507+ $NotAllowedList -contains ' Severity' -and
508+ $NotAllowedList -contains ' MessageId' -and
509+ $IfEqualParameterList.Ensure -eq ' Absent'
510+ } - Exactly - Times 1 - Scope It
511+ }
512+ }
513+
514+ It ' Should call Assert-BoundParameter with correct parameters when Severity is specified' {
515+ InModuleScope - ScriptBlock {
516+ Mock - CommandName ' Assert-BoundParameter' - MockWith {
517+ # Simulate the Assert-BoundParameter throwing an exception for NotAllowed parameters
518+ if ($NotAllowedList -and ($BoundParameterList.ContainsKey (' Severity' ) -or $BoundParameterList.ContainsKey (' MessageId' ))) {
519+ throw ' Parameter validation failed'
520+ }
521+ }
522+
523+ $properties = @ {
524+ Name = ' TestAlert'
525+ Ensure = ' Absent'
526+ Severity = 16
527+ }
528+
529+ { $script :mockSqlAgentAlertInstance.AssertProperties ($properties ) } | Should - Throw
530+
531+ Should - Invoke - CommandName ' Assert-BoundParameter' - ParameterFilter {
532+ $BoundParameterList -is [hashtable ] -and
533+ $NotAllowedList -contains ' Severity' -and
534+ $NotAllowedList -contains ' MessageId' -and
535+ $IfEqualParameterList.Ensure -eq ' Absent'
536+ } - Exactly - Times 1 - Scope It
435537 }
436538 }
437539 }
0 commit comments