RickGaribay.NET

.NET, WCF, SOA, Phoenix Connected Systems User Group, and miscellaneous discussions on software engineering, process and practices.
posts - 142, comments - 91, trackbacks - 4

Unit Testing Private Methods

We had a discussion today around how to test private methods in unit tests. I could have sworn I blogged about this about a year ago but I guess not.

Anyway, let's say you have Implementation.dll that contains a private method. Within the AssemblyInfo.cs file for that assembly, simply add the InternalsVisibleTo attribute, naming the assembly that will be making the calls into the implementation.

For example, if MyUnitTests.dll will call private methods on a class in Implementation.dll, add the following to the AssemblyInfo.cs file for Implementation.dll:

[assembly: InternalsVisibleTo("MyUnitTestAssembly.dll")]

Scary? Yes, but also very useful if you are truly applying TDD.

More info here: http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

 

Print | posted on Wednesday, August 01, 2007 2:59 PM | Filed Under [ Processs ]

Feedback

Gravatar

# re: Unit Testing Private Methods

Does this work with private methods? I thought in only worked with internal ones.
9/10/2007 7:46 PM | Greg Arzoomanian
Gravatar

# re: Unit Testing Private Methods

This *really* isn't a good, not to mention, safe, way to design code. Instead, use VS' built-in ability to create reflective interfaces to private methods using the "Create Private Accessor" tool.
9/17/2007 10:55 AM | Nathan Smith

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 5 and type the answer here:

Powered by: