Wednesday, October 15, 2014

C#.Net Unit Tests - how to access intenal attributes and methods?

I was testing my code and found that I need to be able to modify some internal attributes and to test internal methods in my Unit Tests.

Fortunately there is a way to this!

It is very well documented in the links below:

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

http://devlicio.us/blogs/derik_whittaker/archive/2007/04/09/internalsvisibleto-testing-internal-methods-in-net-2-0.aspx

Just a note: make sure you use internal, and not private access modifiers, and also, if your assembly name and namespace are different - make sure use Assembly Name in

[assembly: InternalsVisibleTo("XXXTests")]

You can find the Assembly Name in the project properties, Application tab.