Sunday, February 11, 2007

Unit Testing Internal Types (2)

It works!

In a previous article, I discussed unit testing internal classes which are typically instantiated using Reflection and are accessed via a Facade.

Learnings: When TestDriven.NET runs NUnit tests through the IDE, configuration values seem to be pulled from testassembly.dll.config.temp, while the specified configuration file is used if run through the NUnit GUI.

These files would have to be replaced with our custom content, to test the internal component, through the Facade.

Anyway, I scrapped together a helper class that would handle copying of the configuration content (overloads for config content as a string and alternatively, a source config file). The [TestFixtureTearDown] method is used to restore the original configuration, while individual [Test]s can use the helper to copy the relevant config file for running.

I'm glad that it worked, but given that it's a hacked together solution, could stop working with any forthcoming release of NUnit (e.g. if config files are locked for the duration of execution etc.).

Steven Cohn (in his blog) made good sense about not modifying the 'System Under Test', in order to test it. The only thing I've got to say about 'pragmatic unit testing' is, given all the restrictions in testing internal types, the most pragmatic thing would be to stop unit testing... :P

You'd think I'd be happy at having solved the problem (hack or otherwise), but strangely, jumping through hoops does sweet nothing for the temper! ;-)

No comments: