To put it in the most simple words, IMO testing what method does not do might be very slippery, as you can come up with more and more scenarios when you think about it. Going other way around tho, asserting that your code does stuff you intended it to do is pretty much purpose of unit testing.
There are two simple questions which usually help me spotting suspicious test and dealing with figuring out whether test makes any sense:
- what part of desired functionality is test exercising?
- what simple change can I make in tested class to break test?
Note that it's extremely easy to deal with those questions having second test (_CorrectCount
) in mind. We haven't really seen Add
method code, but we can most likely produce decent guess what could be changed to break that test. Tested functionality is even more obvious. Answers are intuitive and appear fast (which is good!).
Now let's try to answer those questions for the first test (_NoException
). It immediately raises new questions (Is working code an actual functionality? Isn't it obvious? Isn't that implied? Isn't that what we always strive for? Why there is no assertion at the end? How can I make it fail?). For the second question it's even worse - breaking that test would probably require explicitly throwing exception... which we all agree is not the way to go.
Conclusion
Is simple. Second test is perfect example of well-written unit test. It's short, it tests single thing, it can be easily figured out. First test is not. Even tho it is just as short and (what seems to be) simple, it introduces new questions (while it really should answer already stated ones - Does Add
actually add? Yes.) - and as a result brings unnecessary complexity.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…