We had a new team lead start recently, he seems to have had a fair amount of experience in areas I’m only vaguely familiar with, mostly through reading. One of the first things being pushed for is a concentration on unit testing. While I did begin implementing some tests into our codebase a few months ago (around 250 so far), I feel that there’s still a long way to go. Luckily Chris is here to help impart knowledge to us, hooray!
So getting started with unit testing - first it should be defined what a unit test is.
In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.
Given the following classes/methods:
Note in the above, I am using interfaces to allow for the injection of dependencies (an important part of unit testing with mocks, and in general). The basic idea is you provide sample (unimportant) implementations to the dependent pieces of the whole, the pieces that are not currently being tested, therefore are unimportant to the test - at least when testing the “Put it all together” methods.
I see the following things that need to be tested - there could very well be more, but here it is at a glance:
There are a few other stragglers in there that will become apparent (if they aren’t already) like null testing parameters in the constructor, testing empty array for add numbers, etc.
INumbersFunctions.AddNumbers, we need to of course, check the numbers are being added properly. I have accomplished that with the following tests:
There is most definitely some overlap in some of the tests, but with something so simple there’s still quite a few! I don’t feel the actual implementation of NumberFunctions is important, as I’m concentrating on the tests.
That takes care of the class that has completed non Moq-ed tests. In the next post (which I will hopefully do soon) I’ll go into how I accomplished my first unit tests with Moq. If I can get a well enough cadence going on, I hope to cover Moq-ing WCF service calls - as that’s what we use at work for our communication to our DB, so being able to moq that as well would be beneficial.
Full code including the Moq unit tests can be found at: https://github.com/Kritner/RussUnitTestSample