Unit testing simple void method

I have a lot of methods that take some arguments (let's call them M), validate those arguments (throwing ArgumentNullExceptions and what not) and do a database call. That's it. Only a few lines of code. To allow unit testing, to the constructor of their class I pass an interface / abstract class that the methods then call for database access. This allows me to implement a fake data handler in my unit tests that I can use without worrying about database access.

So my unit tests are like this:

  1. Using the Moq framework, create a mock IDataHandler and mark the relevant method as Verifiable (basically it will let you verify that this method was called in the unit test).
  2. Call M, which validates the arguments and calls the mock IDataHandler, which fakes a database call.
  3. Verify that the method that was supposed to be called on IDataHandler in step 2 was actually called.

And I also have a few unit tests for edge cases, so for example I have a unit test that expects an exception to be thrown etc.

My question is: is my approach correct / reasonable for this type of methods?

by superdraco via /r/csharp

Leave a Reply