Recently we’ve been looking at improving our unit test code coverage and reducing the amount of duplicated code around our bespoke data access layer. Where possible we have moved over to NHibernate but certain parts of the data access must still be written using the standard ADO.NET connection/command pattern. Typically hidden right in the middle of this bespoke code is a while loop that is pivoting a data reader into a POCO that is impossible to repeat-ably unit test in a stable environment unless you set up a dedicated data repository for testing or try to wrap up / mock the connection / command objects. Neither of these options are really desirable as we aren’t really interested in testing / mocking the .NET provider data access objects.
To get around this issue we looked into how we could generate some boiler-plate code that we could roll out across our code base. This code base will be introduced step by step during this series with the first step covering the abstraction of the data reader processing into a standalone object that can be tested in isolation of the data access code. The example code that we are looking to migrate is shown below, a typical unrestricted “Get()” call (in this example we don’t have many customers!).
Hidden below (and behind) a concrete implementation of a SqlConnection and and SqlCommand is the code that we are interested in right now:
As previously stated, unless we set up a test repository or find a way to wrap / mock these concrete instances we are unable to test the creation from the data reader of the customer POCO. To rectify this we start with an interface defining how we would like the calling code to convert the passed in data reader:
Taking a very basic customer POCO object:
An implementation of the IConvertDataReader interface can be created quite simply using the code below. The validation / error checking around each data reader value conversion could be as simple or as complex as you need. Part two of this series will cover a basic optimisation that can be made to this code to potentially speed up access in repeated calls such as a “while(dataReader.Read())” loop.
This data reader convertor can now be unit tested in isolation of the code that will be implementing it, using a combination NUnit and Mock to both assert actual returned results and verify expected behaviour. It’s probably worth highlighting at this stage we are using a combination of MsTest and NUnit, doing things this way brings the best of both worlds – in TFS you get automated Unit Testing as part of the CI build as you are referencing MsTest, but by adding an alias to NUnit’s assert you are getting access to NUnit’s fluent API (which we prefer).
Obviously the above is a very basic test, but any solution can be scaled to reflect any addition or complex processing that may take place in the implementation of Parse(IDataReader dataReader) method. In fact in part two of this series we will highlight how we can implement some potential implementation and verify the expected behaviour whilst confirming that the returned results have not changed.
Finally to wrap up part one of this series here is the original code updated to reflect the changes discussed here. We’ve not really changed a lot, but are now able to unit test code that was previously difficult to easily reach.
Part 2 of this series shows how the code covered in this post can easily be optimised to increase performance, with tests updated to reflect the change.