State Street Gang
.NET, straight up

C# Next: How about some integral testing support?

January 4, 2008 15:14 by Will

So I'm sitting here at work, reviewing mock frameworks like Rhino Mocks and TypeMock for use in our unit tests, when an actual useful idea for the next verison of the CLR pops into my head. 

One of the problems with unit testing is that the requirements of the tests are often polar opposite of object oriented best practices.  Some of these, like data hiding, are relatively easy to overcome in .NET using features such as reflection.  Others aren't so easy to deal with. Im in ur code mockin ur objex

For instance, say you have a sealed class in your application that doesn't implement an interface.  How do you mock that class?  The TL;DR answer to that question is that you can't do it without rewriting your class or using expensive and complicated testing tools.  The second option is often out of the question with small shops like mine.  The first option is viable, but complicates your code and can end up breaking some OO principles or best practices to do so. 

What would be real slick is if the CLR team provided some first class support for testing.  For the example above, it could be the ability to override sealed to allow classes to be extended for mocking.  Another feature might be a SuperNew, that allows child classes to override base class methods even when the object is cast to the base class type.  Yet another may be allowing extension methods to replace methods in an object.  That might be the simplest way of all to mock an object.  Combine the replacement methods with classes designed to record method arguments and play back return values and unit testing becomes a breeze.

These changes, of course, can be a security and stability risk.  But so can using reflection to manipulate private object fields.  But there isn't any reason why these facilities can't be turned on and off via, for example, compiler switches.  In this case, it could be as easy to control as setting a flag within a configuration (say Debug). 

Ah, well, we can dream, can't we?  Perhaps the CLR team will concentrate on some other important additions to the platform, like cupholders and vibrating, heated seats. 


Tags:
Categories: Testing
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Comments are closed