I'm a C# developer having worked with .Net since it was in beta. Before that I mainly worked in C and C++. I have been developing commercial software for more than 20 years. I also mess around with microprocessors, but that's just for fun. I live near Cambridge, England and work from home in my 'silicon shed'.

Blogroll

Following on from my NullOf<T> class, I decided to write something that enables me to throw an exception from any method on an interface. That meant I could easily write unit tests to check exception handling. It does a similar job to the “null implementation” of an interface, but allows you to specify that a certain method will raise an exception. This is what I’ve got:

So this example throws an exception when you call ILog.Info(), but not when you call any of the other methods on the ILog interface. Again, it’s generic, so you can point it at other interfaces and make them throw exceptions too.

People who know me understand that I like pretty code. This post describes something that I came up with to make my units tests look more pretty. Say you want to run a unit test on a service, and normally that service would log lots of stuff (using log4net for example). But for the purpose of the unit test you just want to throw away all the log entries. You might use moq, and do something like this:

Ahh, we still have a “null” implementation of the ILog interface, but to me saying “NullOf<log4net.ILog>.Instance“ looks just a bit nicer :-) Better than that, it’s generic, so you can use it on other interfaces too. Null implementations all round then.