Unit Testing: Exposing Internal/Friend Members
Visual Studio 2008 (Professional Edition and above) provides a really nice set of tools for development and execution of unit tests. It is, of course, easy for your tests to access the public properties and methods of your classes. But what about those internal/friend members?
[To begin with an overview of unit testing, start here.]
[To expose private members, see this post.]
You may have properties or methods in your class defined to be internal (C#) or friend (VB). This post details how to test those methods using the unit testing features in Visual Studio.
Well, it is easy. You just need to use this line of code:
In C#:
[assembly: InternalsVisibleTo("BoTest")]
In VB:
<Assembly: InternalsVisibleTo("BoTest")>
Where BoTest is the name of your unit testing project.
The hard part is finding where you put this line of code. It should reside in your AssemblyInfo file.
This file is relatively easy to find in your C# project; not so easy to find in your VB project. Here are the steps for both.
In C#:
- Find the project you want to test in Solution Explorer.
- Open the Properties node.
- Double-click on the AssemblyInfo.cs file.
- Set the InternalsVisibleTo attribute into the AssemblyInfo.cs file.
- Rebuild the project.
In VB:
- Find the project you want to test in Solution Explorer.
- Click on the Show All Files button in the Solution Explorer toolbar.
- Open the My Project node
- Double-click on the AssemblyInfo.vb file.
- Set the InternalsVisibleTo attribute into the AssemblyInfo.vb file.
Use this technique whenever you have internal/friend members in a class that you wish to access with your unit testing code.
Enjoy!