Skip to content

Commit 20d9944

Browse files
Copilotsamsmithnz
andcommitted
Improve test assertions and refactor test methods
- Replace Assert.IsTrue with Assert.AreEqual for clearer failure messages - Rename test methods for clarity (ACUS -> AmericanCup, 1CubicCM -> 1000CubicCM) - Add shared controller instance using TestInitialize in SkittleControllerTests - Use Assert.Throws<Exception> for exception testing (MSTest 4.x compatible) - Fix quantity type consistency (1 -> 1f) Co-authored-by: samsmithnz <8389039+samsmithnz@users.noreply.github.com>
1 parent a3c2cf9 commit 20d9944

File tree

5 files changed

+74
-122
lines changed

5 files changed

+74
-122
lines changed

src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class PeanutMandMControllerTests
1111
#region "Testing units"
1212

1313
[TestMethod]
14-
public void ControllerCountPeanutMandMsInAUSCupTest()
14+
public void ControllerCountPeanutMandMsInAmericanCupTest()
1515
{
1616
//Arrange
1717
string unit = "Cup";
@@ -22,15 +22,15 @@ public void ControllerCountPeanutMandMsInAUSCupTest()
2222
float result = controller.GetDataForUnit(unit, quantity);
2323

2424
//Assert
25-
Assert.IsTrue(System.Math.Round(result, 0) == 181f);
25+
Assert.AreEqual(181f, System.Math.Round(result, 0));
2626
}
2727

2828
#endregion
2929

3030
#region " Testing volume in a rectangle"
3131

3232
[TestMethod]
33-
public void ControllerCountPeanutMandMsInA1CubicCMTest()
33+
public void ControllerCountPeanutMandMsInA1000CubicCMTest()
3434
{
3535
//Arrange
3636
string unit = "cm";
@@ -43,7 +43,7 @@ public void ControllerCountPeanutMandMsInA1CubicCMTest()
4343
float result = controller.GetDataForRectangle(unit, height, width, length);
4444

4545
//Assert
46-
Assert.IsTrue((int)System.Math.Round(result, 0) == (int)764f);
46+
Assert.AreEqual(764, (int)System.Math.Round(result, 0));
4747
}
4848

4949
#endregion
@@ -63,7 +63,7 @@ public void ControllerCountPeanutMandMsInACylinderWithCMTest()
6363
float result = controller.GetDataForCylinder(unit, height, radius);
6464

6565
//Assert
66-
Assert.IsTrue(System.Math.Round(result, 0) == 600f);
66+
Assert.AreEqual(600f, System.Math.Round(result, 0));
6767
}
6868

6969
#endregion

src/MandMCounter.Tests/Controllers/SkittleControllerTests.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ namespace MandMCounter.Tests.Controllers
88
[TestClass]
99
public class SkittleControllerTests
1010
{
11+
private SkittleCounterController _controller;
12+
13+
[TestInitialize]
14+
public void Setup()
15+
{
16+
_controller = new SkittleCounterController();
17+
}
18+
1119
#region "Testing units"
1220

1321
[TestMethod]
@@ -16,13 +24,15 @@ public void ControllerCountSkittlesInAUSCupTest()
1624
//Arrange
1725
string unit = "Cup";
1826
float quantity = 1f;
27+
const float expectedSkittles = 212f;
28+
const float tolerance = 0.0001f;
1929

2030
//Act
21-
SkittleCounterController controller = new SkittleCounterController();
22-
float result = controller.GetDataForUnit(unit, quantity);
31+
//SkittleCounterController controller = new SkittleCounterController();
32+
float result = _controller.GetDataForUnit(unit, quantity);
2333

2434
//Assert
25-
Assert.IsTrue(System.Math.Abs(System.Math.Round(result, 0) - 212f) < 0.0001f);
35+
Assert.AreEqual(expectedSkittles, System.Math.Round(result, 0), tolerance);
2636
}
2737

2838
#endregion
@@ -39,11 +49,13 @@ public void ControllerCountSkittlesInA1CubicCMTest()
3949
float length = 10;
4050

4151
//Act
42-
SkittleCounterController controller = new SkittleCounterController();
43-
float result = controller.GetDataForRectangle(unit, height, width, length);
52+
//SkittleCounterController controller = new SkittleCounterController();
53+
float result = _controller.GetDataForRectangle(unit, height, width, length);
4454

4555
//Assert
46-
Assert.IsTrue(System.Math.Abs(System.Math.Round(result, 0) - 896f) < 0.0001f);
56+
const float expected = 896f;
57+
const float delta = 0.0001f;
58+
Assert.AreEqual(expected, System.Math.Round(result, 0), delta);
4759
}
4860

4961
#endregion
@@ -59,8 +71,8 @@ public void ControllerCountSkittlesInACylinderWithCMTest()
5971
float radius = 5;
6072

6173
//Act
62-
SkittleCounterController controller = new SkittleCounterController();
63-
float result = controller.GetDataForCylinder(unit, height, radius);
74+
//SkittleCounterController controller = new SkittleCounterController();
75+
float result = _controller.GetDataForCylinder(unit, height, radius);
6476

6577
//Assert
6678
Assert.IsTrue(System.Math.Abs(System.Math.Round(result, 0) - 704f) < 0.0001f);

src/MandMCounter.Tests/MandMTests.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void CountMandMsInAUSTableSpoonTest()
101101
{
102102
//Arrange
103103
string unit = "Tablespoon";
104-
float quantity = 1;
104+
float quantity = 1f;
105105

106106
//Act
107107
float result = Calculator.CountMandMs(unit, quantity);
@@ -115,7 +115,7 @@ public void CountMandMsInAUSTeaSpoonTest()
115115
{
116116
//Arrange
117117
string unit = "Teaspoon";
118-
float quantity = 1;
118+
float quantity = 1f;
119119

120120
//Act
121121
float result = Calculator.CountMandMs(unit, quantity);
@@ -127,20 +127,12 @@ public void CountMandMsInAUSTeaSpoonTest()
127127
[TestMethod]
128128
public void CountMandMsInANullUnitTest()
129129
{
130-
try
131-
{
132-
//Arrange
133-
string unit = null;
134-
float quantity = 1;
130+
//Arrange
131+
string unit = null;
132+
float quantity = 1f;
135133

136-
//Act
137-
Calculator.CountMandMs(unit, quantity);
138-
}
139-
catch (Exception ex)
140-
{
141-
//Assert
142-
Assert.IsTrue(ex != null);
143-
}
134+
//Act & Assert
135+
Assert.Throws<Exception>(() => Calculator.CountMandMs(unit, quantity));
144136
}
145137

146138
[TestMethod]

src/MandMCounter.Tests/PeanutMandMTests.cs

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,12 @@ public void CountPeanutMandMsInAUSTeaSpoonTest()
104104
[TestMethod]
105105
public void CountPeanutMandMsInANullUnitTest()
106106
{
107-
try
108-
{
109-
//Arrange
110-
string unit = null;
111-
float quantity = 1;
112-
113-
//Act
114-
115-
Calculator.CountPeanutMandMs(unit, quantity);
116-
}
117-
catch (Exception ex)
118-
{
119-
//Assert
120-
Assert.IsTrue(ex != null);
121-
}
107+
//Arrange
108+
string unit = null;
109+
float quantity = 1;
110+
111+
//Act & Assert
112+
Assert.Throws<Exception>(() => Calculator.CountPeanutMandMs(unit, quantity));
122113
}
123114

124115
[TestMethod]
@@ -178,23 +169,17 @@ public void CountPeanutMandMsInA1CubicInchTest()
178169
[TestMethod]
179170
public void CountPeanutMandMsInA1CubicNullUnitTest()
180171
{
181-
try
182-
{
183-
//Arrange
184-
string unit = null;
185-
float height = 1;
186-
float width = 1;
187-
float length = 1;
188-
189-
//Act
190-
191-
float result = Calculator.CountPeanutMandMs(unit, height, width, length);
192-
}
193-
catch (Exception ex)
172+
//Arrange
173+
string unit = null;
174+
float height = 1;
175+
float width = 1;
176+
float length = 1;
177+
178+
//Act & Assert
179+
Assert.Throws<Exception>(() =>
194180
{
195-
//Assert
196-
Assert.IsTrue(ex != null);
197-
}
181+
Calculator.CountPeanutMandMs(unit, height, width, length);
182+
});
198183
}
199184

200185
#endregion
@@ -236,22 +221,16 @@ public void CountPeanutMandMsInACylinderWithInchTest()
236221
[TestMethod]
237222
public void CountPeanutMandMsInACylinderWithNullUnitTest()
238223
{
239-
try
240-
{
241-
//Arrange
242-
string unit = null;
243-
float height = 10;
244-
float radius = 5;
245-
246-
//Act
247-
248-
float result = Calculator.CountPeanutMandMs(unit, height, radius);
249-
}
250-
catch (Exception ex)
224+
//Arrange
225+
string unit = null;
226+
float height = 10;
227+
float radius = 5;
228+
229+
//Act & Assert
230+
Assert.Throws<Exception>(() =>
251231
{
252-
//Assert
253-
Assert.IsTrue(ex != null);
254-
}
232+
Calculator.CountPeanutMandMs(unit, height, radius);
233+
});
255234
}
256235

257236
#endregion

src/MandMCounter.Tests/SkittleTests.cs

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,12 @@ public void CountSkittlesInAUSTeaSpoonTest()
104104
[TestMethod]
105105
public void CountSkittlesInANullUnitTest()
106106
{
107-
try
108-
{
109-
//Arrange
110-
string unit = null;
111-
float quantity = 1;
112-
113-
//Act
114-
115-
Calculator.CountSkittles(unit, quantity);
116-
}
117-
catch (Exception ex)
118-
{
119-
//Assert
120-
Assert.IsTrue(ex != null);
121-
}
107+
//Arrange
108+
string unit = null;
109+
float quantity = 1;
110+
111+
//Act & Assert
112+
Assert.Throws<Exception>(() => Calculator.CountSkittles(unit, quantity));
122113
}
123114

124115
[TestMethod]
@@ -178,23 +169,12 @@ public void CountSkittlesInA1CubicInchTest()
178169
[TestMethod]
179170
public void CountSkittlesInA1CubicNullUnitTest()
180171
{
181-
try
182-
{
183-
//Arrange
184-
string unit = null;
185-
float height = 1;
186-
float width = 1;
187-
float length = 1;
188-
189-
//Act
190-
191-
float result = Calculator.CountSkittles(unit, height, width, length);
192-
}
193-
catch (Exception ex)
194-
{
195-
//Assert
196-
Assert.IsTrue(ex != null);
197-
}
172+
string unit = null;
173+
float height = 1;
174+
float width = 1;
175+
float length = 1;
176+
177+
Assert.Throws<Exception>(() => Calculator.CountSkittles(unit, height, width, length));
198178
}
199179

200180
#endregion
@@ -236,22 +216,11 @@ public void CountSkittlesInACylinderWithInchTest()
236216
[TestMethod]
237217
public void CountSkittlesInACylinderWithNullUnitTest()
238218
{
239-
try
240-
{
241-
//Arrange
242-
string unit = null;
243-
float height = 10;
244-
float radius = 5;
245-
246-
//Act
247-
248-
float result = Calculator.CountSkittles(unit, height, radius);
249-
}
250-
catch (Exception ex)
251-
{
252-
//Assert
253-
Assert.IsTrue(ex != null);
254-
}
219+
string unit = null;
220+
float height = 10;
221+
float radius = 5;
222+
223+
Assert.Throws<Exception>(() => Calculator.CountSkittles(unit, height, radius));
255224
}
256225

257226
#endregion

0 commit comments

Comments
 (0)