Testability and mocking

Jan 27, 2009 at 11:29 AM
Hi, I noticed that the framework only uses static methods for nearly all the important calls.
What I want is to be able to do unit testing and use a mocking framework.
Is there a way to configure the framework so that it's possible to use non-static methods and be able to inject a fake SQL service instead of goding directly to the database?
Jan 28, 2009 at 12:26 PM
You are correct - it definitely suffers from static cling! There isn't any way to configure it not to either. If you're willing to run the trunk version versus the latest release, there is a seam available so you can tell the framework to use a different ISqlHelper factory (an interface created for this purpose). Look in the recent check ins for the ServiceLocator class we added.

It's not ideal, and mocking IDataReader is not fun - especially the way the framework uses it to grab metadata about the database tables, but you can do it.

Personally, I think with the framework the way it is now, it's much easier to do integration tests than unit tests. One way to do that is to manage your own SqlHelper / transaction in your tests. Most of the queries that update data have an overload that takes an instance of the helper, so they all can participate in the same transaction. by doing this, at the end of the test, you rollback your transaction, and your database is left untouched. Not ideal, but it will work. Here's a post that demonstrates that:


I know it's not the best answer, but it's where the framework is at right now.