Month: February 2014

Testing the queries in your data access layer

Posted on Updated on

When it comes to writing automated tests I have, in the past, tended to ignore the data access layer. Do you really need to test your queries? As long as the code was loosely coupled and I could test everything else, I was happy. After talking at work I was convinced that, in some cases, it might be worth testing your queries themselves in order to check that they’re returning the data that they should be.

This provided a dilemma, because I will always try to avoid writing integration/database tests if I can help it, but how do you test queries without a database? A possible answer is as follows, taken from my post on StackOverflow:


After some deliberation, my colleague and I came up with a way to separate the query from the persistence layer. I threw together a quick example below:

Read the rest of this entry »