Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

October 29, 2009

Unit Testing: Exposing Internal/Friend Members

Filed under: C#,Testing,VB.NET @ 5:23 pm

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#:

  1. Find the project you want to test in Solution Explorer.
  2. Open the Properties node.
  3. Double-click on the AssemblyInfo.cs file.
  4. Set the InternalsVisibleTo attribute into the AssemblyInfo.cs file.
  5. Rebuild the project.

In VB:

  1. Find the project you want to test in Solution Explorer.
  2. Click on the Show All Files button in the Solution Explorer toolbar.
  3. Open the My Project node
  4. Double-click on the AssemblyInfo.vb file.
  5. 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.


RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs