Reputation: 441
I've just finished reading "Professional Test-Driven Development with C#" and have been trying to find a way to achieve 100% coverage in my code. It's all good until I hit a repository class that is filled with methods implemented something like this:
public IEnumerable<MyDataContract> LoadConditional(bool isCondition)
{
const string QUERY = @"SELECT ...fields... FROM MyData WHERE [IsCondition] = @IsCondition";
return DataAccessor.ReadMyContracts(QUERY, isCondition); // something, something...
}
I've been thinking about this for some time, and have not been able to find an answer on the internet that answers this question directly.
I read things that would suggest that SQL related business would exist in another assembly. I don't require this though and don't believe I should have to go there. And this, from a code coverage, perspective is a pretty superficial change.
I've read that you can hook up databases for your unit tests (which I've done before). But this just well... I dunno, it doesn't feel right. The tests are slow and have a significant increase in maintenance.
My gut feeling is that without the last bit I mentioned, this method can't be unit tested. How should I be viewing this problem?
Upvotes: 1
Views: 512
Reputation: 10561
The way I see it, if you do actual data access, you are going in to integration testing and leaving the realm of unit testing. I personaly prefer to keep SQL inside the data access layer only, i.e. a single layer before the DB itself and then I test everything up to that point. In my view a method named ReadMyContracts
should already have the correct SQL for accessing the data and should only receive (and pass on) the isCondition
parameter.
That`s just MHO.
Upvotes: 0
Reputation: 14591
First let me say that I believe that achieving 100% coverage makes no sense at all and doesn't prove anything.
That being said, I typically use some layer between DB and business logic - some simple mapper (PetaPoco, Dapper, OrmLite) or, rarely, a full blown ORM (NHibernate).
In cases where I need integration tests against a DB, these tools allow me to run the same queries against a test DB (e.g. an in-memory SQLite DB) instead of 'real' DB server.
With regard to your concern that "the tests are slow and have a significant increase in maintenance." you should bear in mind that these are not unit tests anymore - these are integration tests and they can't be as fast as unit tests.
Upvotes: 6