@@ -63,13 +63,20 @@ public void Constructor_WithValidKeySelector_CreatesInstance()
6363 }
6464
6565 [ TestMethod ]
66- [ ExpectedException ( typeof ( ArgumentNullException ) ) ]
66+
67+
6768 public void Constructor_WithNullKeySelector_ThrowsArgumentNullException ( )
69+
70+
6871 {
69- // Arrange & Act
70- var keyedList = new KeyedList < int , TestItem > ( null ! ) ;
7172
72- // Assert: 期望抛出 ArgumentNullException
73+
74+ // Arrange, Act & Assert
75+
76+
77+ Assert . ThrowsException < ArgumentNullException > ( ( ) => new KeyedList < int , TestItem > ( null ! ) ) ;
78+
79+
7380 }
7481
7582 #endregion
@@ -87,13 +94,20 @@ public void Indexer_GetWithValidIndex_ReturnsCorrectItem()
8794 }
8895
8996 [ TestMethod ]
90- [ ExpectedException ( typeof ( ArgumentOutOfRangeException ) ) ]
97+
98+
9199 public void Indexer_GetWithInvalidIndex_ThrowsArgumentOutOfRangeException ( )
100+
101+
92102 {
93- // Arrange & Act
94- var item = _keyedList [ - 1 ] ;
95103
96- // Assert: 期望抛出 ArgumentOutOfRangeException
104+
105+ // Arrange, Act & Assert
106+
107+
108+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => _keyedList [ - 1 ] ) ;
109+
110+
97111 }
98112
99113 [ TestMethod ]
@@ -110,16 +124,27 @@ public void Indexer_SetWithValidIndex_UpdatesItem()
110124 }
111125
112126 [ TestMethod ]
113- [ ExpectedException ( typeof ( ArgumentOutOfRangeException ) ) ]
127+
128+
114129 public void Indexer_SetWithInvalidIndex_ThrowsArgumentOutOfRangeException ( )
130+
131+
115132 {
133+
134+
116135 // Arrange
136+
137+
117138 var newItem = new TestItem { Id = 10 , Name = "New Item" } ;
118139
119- // Act
120- _keyedList [ 10 ] = newItem ;
121140
122- // Assert: 期望抛出 ArgumentOutOfRangeException
141+
142+ // Act & Assert
143+
144+
145+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => _keyedList [ 10 ] = newItem ) ;
146+
147+
123148 }
124149
125150 #endregion
@@ -375,39 +400,30 @@ public void CopyTo_ValidArrayWithOffset_CopiesItemsWithOffset()
375400 }
376401
377402 [ TestMethod ]
378- [ ExpectedException ( typeof ( ArgumentNullException ) ) ]
379403 public void CopyTo_NullArray_ThrowsArgumentNullException ( )
380404 {
381- // Arrange & Act
382- _keyedList . CopyTo ( null ! , 0 ) ;
383-
384- // Assert: 期望抛出 ArgumentNullException
405+ // Arrange, Act & Assert
406+ Assert . ThrowsException < ArgumentNullException > ( ( ) => _keyedList . CopyTo ( null ! , 0 ) ) ;
385407 }
386408
387409 [ TestMethod ]
388- [ ExpectedException ( typeof ( ArgumentOutOfRangeException ) ) ]
389410 public void CopyTo_NegativeArrayIndex_ThrowsArgumentOutOfRangeException ( )
390411 {
391412 // Arrange
392413 var array = new TestItem [ _keyedList . Count ] ;
393414
394- // Act
395- _keyedList . CopyTo ( array , - 1 ) ;
396-
397- // Assert: 期望抛出 ArgumentOutOfRangeException
415+ // Act & Assert
416+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => _keyedList . CopyTo ( array , - 1 ) ) ;
398417 }
399418
400419 [ TestMethod ]
401- [ ExpectedException ( typeof ( ArgumentException ) ) ]
402420 public void CopyTo_InsufficientArraySpace_ThrowsArgumentException ( )
403421 {
404422 // Arrange
405423 var array = new TestItem [ _keyedList . Count - 1 ] ;
406424
407- // Act
408- _keyedList . CopyTo ( array , 0 ) ;
409-
410- // Assert: 期望抛出 ArgumentException
425+ // Act & Assert
426+ Assert . ThrowsException < ArgumentException > ( ( ) => _keyedList . CopyTo ( array , 0 ) ) ;
411427 }
412428
413429 #endregion
@@ -482,29 +498,23 @@ public void Insert_ValidIndex_InsertsItemAtCorrectPosition()
482498 }
483499
484500 [ TestMethod ]
485- [ ExpectedException ( typeof ( ArgumentOutOfRangeException ) ) ]
486501 public void Insert_NegativeIndex_ThrowsArgumentOutOfRangeException ( )
487502 {
488503 // Arrange
489504 var newItem = new TestItem { Id = 4 , Name = "Item 4" } ;
490505
491- // Act
492- _keyedList . Insert ( - 1 , newItem ) ;
493-
494- // Assert: 期望抛出 ArgumentOutOfRangeException
506+ // Act & Assert
507+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => _keyedList . Insert ( - 1 , newItem ) ) ;
495508 }
496509
497510 [ TestMethod ]
498- [ ExpectedException ( typeof ( ArgumentOutOfRangeException ) ) ]
499511 public void Insert_IndexGreaterThanCount_ThrowsArgumentOutOfRangeException ( )
500512 {
501513 // Arrange
502514 var newItem = new TestItem { Id = 4 , Name = "Item 4" } ;
503515
504- // Act
505- _keyedList . Insert ( _keyedList . Count + 1 , newItem ) ;
506-
507- // Assert: 期望抛出 ArgumentOutOfRangeException
516+ // Act & Assert
517+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => _keyedList . Insert ( _keyedList . Count + 1 , newItem ) ) ;
508518 }
509519
510520 #endregion
@@ -562,23 +572,17 @@ public void RemoveAt_ValidIndex_RemovesItemAtIndex()
562572 }
563573
564574 [ TestMethod ]
565- [ ExpectedException ( typeof ( ArgumentOutOfRangeException ) ) ]
566575 public void RemoveAt_NegativeIndex_ThrowsArgumentOutOfRangeException ( )
567576 {
568- // Act
569- _keyedList . RemoveAt ( - 1 ) ;
570-
571- // Assert: 期望抛出 ArgumentOutOfRangeException
577+ // Act & Assert
578+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => _keyedList . RemoveAt ( - 1 ) ) ;
572579 }
573580
574581 [ TestMethod ]
575- [ ExpectedException ( typeof ( ArgumentOutOfRangeException ) ) ]
576582 public void RemoveAt_IndexEqualToCount_ThrowsArgumentOutOfRangeException ( )
577583 {
578- // Act
579- _keyedList . RemoveAt ( _keyedList . Count ) ;
580-
581- // Assert: 期望抛出 ArgumentOutOfRangeException
584+ // Act & Assert
585+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => _keyedList . RemoveAt ( _keyedList . Count ) ) ;
582586 }
583587
584588 #endregion
0 commit comments