Tech Journal Back to Tech Journal

How do I do testing on internal members of C# classes?

[There's a nice article about this at http://smsohan.blogspot.com/2008/03/testing-internal-class-in-net-c.html]

The Problem: How do we access the internal state of an assembly when we are testing it from a Unit Testing assembly.

Solution: Declare some internal members that can report to the extenral UnitTest what's happening inside the tested assembly.

Once you've declare members as internal, all you need to do is modify the assembly.cs file to add the directive to expose the assembly's internals to the unit-testing assembly:

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("UnitTestAssemblyName")]

The trick, of course, is figuring out what to declare as private, and what to expose at internal for testing, and what to export to the world as public.

Last updated on 2009-03-19 14:41:53 -0800, by Shalom Craimer

Back to Tech Journal