Skip to content

Commit 97c2433

Browse files
committed
Replace ExpectedException with ThrowsExactly in tests
Updated unit tests to use Assert.ThrowsExactly instead of ExpectedException attributes and Assert.ThrowsException calls. This change improves test reliability and clarity by ensuring the exact exception type is asserted, and modernizes the test code to current best practices.
1 parent 5215c84 commit 97c2433

37 files changed

Lines changed: 466 additions & 530 deletions

tests/CommunityToolkit.Common.UnitTests/Extensions/Test_ArrayExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public void Test_ArrayExtensions_Jagged_GetColumn_Exception()
3737
new int[] { 7 }
3838
};
3939

40-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
40+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
4141
{
4242
_ = array.GetColumn(-1).ToArray();
4343
});
4444

45-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
45+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
4646
{
4747
_ = array.GetColumn(3).ToArray();
4848
});

tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.Array.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ public void Test_Guard_IsEmpty_ArrayOk()
1616
}
1717

1818
[TestMethod]
19-
[ExpectedException(typeof(ArgumentException))]
2019
public void Test_Guard_IsEmpty_ArrayFail()
2120
{
22-
Guard.IsEmpty(new int[1], nameof(Test_Guard_IsEmpty_ArrayFail));
21+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.IsEmpty(new int[1], nameof(Test_Guard_IsEmpty_ArrayFail)));
2322
}
2423

2524
[TestMethod]
@@ -29,10 +28,9 @@ public void Test_Guard_IsNotEmpty_ArrayOk()
2928
}
3029

3130
[TestMethod]
32-
[ExpectedException(typeof(ArgumentException))]
3331
public void Test_Guard_IsNotEmpty_ArrayFail()
3432
{
35-
Guard.IsNotEmpty(new int[0], nameof(Test_Guard_IsNotEmpty_ArrayFail));
33+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.IsNotEmpty(new int[0], nameof(Test_Guard_IsNotEmpty_ArrayFail)));
3634
}
3735

3836
[TestMethod]
@@ -42,10 +40,9 @@ public void Test_Guard_HasSizeEqualTo_ArrayOk()
4240
}
4341

4442
[TestMethod]
45-
[ExpectedException(typeof(ArgumentException))]
4643
public void Test_Guard_HasSizeEqualTo_ArrayFail()
4744
{
48-
Guard.HasSizeEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk));
45+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk)));
4946
}
5047

5148
[TestMethod]
@@ -55,10 +52,9 @@ public void Test_Guard_HasSizeNotEqualTo_ArrayOk()
5552
}
5653

5754
[TestMethod]
58-
[ExpectedException(typeof(ArgumentException))]
5955
public void Test_Guard_HasSizeNotEqualTo_ArrayFail()
6056
{
61-
Guard.HasSizeNotEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayFail));
57+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeNotEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayFail)));
6258
}
6359

6460
[TestMethod]
@@ -68,17 +64,15 @@ public void Test_Guard_HasSizeGreaterThan_ArrayOk()
6864
}
6965

7066
[TestMethod]
71-
[ExpectedException(typeof(ArgumentException))]
7267
public void Test_Guard_HasSizeGreaterThan_ArrayEqualFail()
7368
{
74-
Guard.HasSizeGreaterThan(new int[4], 4, nameof(Test_Guard_HasSizeGreaterThan_ArrayEqualFail));
69+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeGreaterThan(new int[4], 4, nameof(Test_Guard_HasSizeGreaterThan_ArrayEqualFail)));
7570
}
7671

7772
[TestMethod]
78-
[ExpectedException(typeof(ArgumentException))]
7973
public void Test_Guard_HasSizeGreaterThan_ArraySmallerFail()
8074
{
81-
Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThan_ArraySmallerFail));
75+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThan_ArraySmallerFail)));
8276
}
8377

8478
[TestMethod]
@@ -89,10 +83,9 @@ public void Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayOk()
8983
}
9084

9185
[TestMethod]
92-
[ExpectedException(typeof(ArgumentException))]
9386
public void Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayFail()
9487
{
95-
Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayFail));
88+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayFail)));
9689
}
9790

9891
[TestMethod]
@@ -102,17 +95,15 @@ public void Test_Guard_HasSizeLessThan_ArrayOk()
10295
}
10396

10497
[TestMethod]
105-
[ExpectedException(typeof(ArgumentException))]
10698
public void Test_Guard_HasSizeLessThan_ArrayEqualFail()
10799
{
108-
Guard.HasSizeLessThan(new int[4], 4, nameof(Test_Guard_HasSizeLessThan_ArrayEqualFail));
100+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeLessThan(new int[4], 4, nameof(Test_Guard_HasSizeLessThan_ArrayEqualFail)));
109101
}
110102

111103
[TestMethod]
112-
[ExpectedException(typeof(ArgumentException))]
113104
public void Test_Guard_HasSizeLessThan_ArrayGreaterFail()
114105
{
115-
Guard.HasSizeLessThan(new int[6], 4, nameof(Test_Guard_HasSizeLessThan_ArrayGreaterFail));
106+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeLessThan(new int[6], 4, nameof(Test_Guard_HasSizeLessThan_ArrayGreaterFail)));
116107
}
117108

118109
[TestMethod]
@@ -123,10 +114,9 @@ public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk()
123114
}
124115

125116
[TestMethod]
126-
[ExpectedException(typeof(ArgumentException))]
127117
public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail()
128118
{
129-
Guard.HasSizeLessThanOrEqualTo(new int[8], 4, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail));
119+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeLessThanOrEqualTo(new int[8], 4, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail)));
130120
}
131121

132122
[TestMethod]
@@ -136,10 +126,9 @@ public void Test_Guard_HasSizeEqualToArray_ArrayOk()
136126
}
137127

138128
[TestMethod]
139-
[ExpectedException(typeof(ArgumentException))]
140129
public void Test_Guard_HasSizeEqualToArray_ArrayFail()
141130
{
142-
Guard.HasSizeEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeEqualToArray_ArrayFail));
131+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeEqualToArray_ArrayFail)));
143132
}
144133

145134
[TestMethod]
@@ -150,9 +139,8 @@ public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk()
150139
}
151140

152141
[TestMethod]
153-
[ExpectedException(typeof(ArgumentException))]
154142
public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail()
155143
{
156-
Guard.HasSizeLessThanOrEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail));
144+
_ = Assert.ThrowsExactly<ArgumentException>(() => Guard.HasSizeLessThanOrEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail)));
157145
}
158146
}

0 commit comments

Comments
 (0)