As always, whether or not a test like that is valuable depends on your motivation for testing.
- Is this piece of code mission-critical?
- What is the cost if that code fails?
- How easily can you address errors, should they occur?
The higher the cost of failure, the more important it is to test a piece of code.
The GetModules
method does at least four things:
- It returns the modules from the DAO.
- It maps the modules from the DAO into the desired return types.
- It returns an error message if something goes wrong.
- It logs any errors that may occur.
The GetModulesTest
tests a single of these four responsibilities, which means that three other tests are still required to fully cover the GetModules
method.
Writing small-grained unit tests are valuable, because it enables you do decompose a complex piece of production code into a set of simple, easy-to-understand unit tests. Sometimes, these unit tests become almost inanely simple, to the point where you'll begin to doubt the value of it, but the value isn't in a single unit test - it's in the accumulation of simple tests, which, together, specify how the entire system ought to work.