One problem with test code is it often has many magic numbers strewn through it values such as 20
or new TimeSpan(1, 30, 15)
. We want our tests, to be readable and easily maintained. Magic numbers such as this often don't tell us much about what's going on in the tests.
Fortunately, extension methods can be added to our test assemblies to make intention more explicit. For example, let's say we wanted to test a class that accepted a number of hours in the constructor and provide a method to add a time span to that original hours. We might think we'd write a test as follows:
[TestMethod] public void TestAddTimespan() { var sut = new TimeCalculator(20); sut.Add(new TimeSpan(1, 30, 15)); Assert.AreEqual(sut.TotalSeconds, 77415); }
Reading through this test long after you wrote it, or if you never wrote it, 20, new TimeSpan(1,30,15)
, and 77415
seem merely like magic numbers. They provide little value. You'd probably need to go look at the constructor for TimeCalculator...