Wednesday, October 22, 2008

Enabling Unit Test Harnesses Against Internal Methods

Being a purist, I like to lock down my classes and methods with private and internal so as not to expose functionality unnecessarily as public. This presents a problem with unit testing. The test harness cannot see internal methods...even on a public class. I had been editing the code to mark the method public, executing tests in debug mode, and then re-editing the method back to internal. Hokey...at best.

As a far more optimal and usable alternative, add an InternalsVisibleTo directive to your AssemblyInfo.cs file. It opens the door to test harnesses to internal methods.

No comments: